REXML: Raw tags in element text?

Discussion in 'Ruby' started by Jari Williamsson, Nov 23, 2007.

  1. In REXML, is it possible to add raw tags to element text, without having
    to add elements for those tags?
    (I would like to add a few HTML tags like <b></b> to strings, before I
    know what element nodes they will appear at.)


    Best regards,

    Jari Williamsson
    Jari Williamsson, Nov 23, 2007
    #1
    1. Advertising

  2. Jari Williamsson

    Phrogz Guest

    On Nov 23, 2:26 pm, Jari Williamsson
    <> wrote:
    > In REXML, is it possible to add raw tags to element text, without having
    > to add elements for those tags?
    > (I would like to add a few HTML tags like <b></b> to strings, before I
    > know what element nodes they will appear at.)


    Are you wanting to ad XML elements as children of another without
    using the DOM to create them (like setting .innerHTML in a web
    browser, causing it to parse your string and create elements as
    necessary),

    or are you wanting to set the contents of a text node to have
    characters that look like XML tags, but have it automatically escape
    them for you?
    Phrogz, Nov 24, 2007
    #2
    1. Advertising

  3. Phrogz wrote:
    > On Nov 23, 2:26 pm, Jari Williamsson
    > <> wrote:
    >> In REXML, is it possible to add raw tags to element text, without having
    >> to add elements for those tags?
    >> (I would like to add a few HTML tags like <b></b> to strings, before I
    >> know what element nodes they will appear at.)

    >
    > Are you wanting to ad XML elements as children of another without
    > using the DOM to create them (like setting .innerHTML in a web
    > browser, causing it to parse your string and create elements as
    > necessary),


    Yes, this is what I want.


    Best regards,

    Jari Williamsson
    Jari Williamsson, Nov 24, 2007
    #3
  4. 2007/11/24, Jari Williamsson <>:
    > Phrogz wrote:
    > > On Nov 23, 2:26 pm, Jari Williamsson
    > > <> wrote:
    > >> In REXML, is it possible to add raw tags to element text, without having
    > >> to add elements for those tags?
    > >> (I would like to add a few HTML tags like <b></b> to strings, before I
    > >> know what element nodes they will appear at.)

    > >
    > > Are you wanting to ad XML elements as children of another without
    > > using the DOM to create them (like setting .innerHTML in a web
    > > browser, causing it to parse your string and create elements as
    > > necessary),

    >
    > Yes, this is what I want.


    I believe you can do it (i.e. add raw tags to text) BUT they will not
    show up as tags when output. Which is completely logical once you
    think about it. If you work with a DOM you have to add nodes as
    nodes. Maybe you can just create a small sub tree with a <b> node and
    your text as nested text and later put that <b> node into the tree.

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
    Robert Klemme, Nov 24, 2007
    #4
  5. Robert Klemme wrote:
    > 2007/11/24, Jari Williamsson <>:
    >> Phrogz wrote:
    >>> On Nov 23, 2:26 pm, Jari Williamsson
    >>> <> wrote:
    >>>> In REXML, is it possible to add raw tags to element text, without having
    >>>> to add elements for those tags?
    >>>> (I would like to add a few HTML tags like <b></b> to strings, before I
    >>>> know what element nodes they will appear at.)
    >>> Are you wanting to ad XML elements as children of another without
    >>> using the DOM to create them (like setting .innerHTML in a web
    >>> browser, causing it to parse your string and create elements as
    >>> necessary),

    >> Yes, this is what I want.

    >
    > I believe you can do it (i.e. add raw tags to text) BUT they will not
    > show up as tags when output. Which is completely logical once you
    > think about it. If you work with a DOM you have to add nodes as
    > nodes. Maybe you can just create a small sub tree with a <b> node and
    > your text as nested text and later put that <b> node into the tree.


    Since nothing seems to exist I wrote my own 2-line method: It adds a raw
    string, parses it and passes all nodes to the parent element. Here's the
    code if anyone else would need it:

    ---
    class REXML::Element
    def add_raw_text(raw_text)
    doc = REXML::Document.new("<fake>"+raw_text+"</fake>")
    doc.root.children.each { |e| self << e }
    end
    end
    ---

    Example:
    myelement.add_raw_text("here's a <b>bold</b> test")


    Best regards,

    Jari Williamsson
    Jari Williamsson, Nov 24, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HANM
    Replies:
    2
    Views:
    694
    Joseph Kesselman
    Jan 29, 2008
  2. Damphyr
    Replies:
    2
    Views:
    131
    Damphyr
    Jul 16, 2003
  3. Daniel Berger

    rexml error - REXML::Validation

    Daniel Berger, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    141
    Henrik Horneber
    Oct 12, 2004
  4. trans.

    [REXML] Raw Elements

    trans., Jan 21, 2005, in forum: Ruby
    Replies:
    12
    Views:
    166
    trans.
    Jan 22, 2005
  5. Phlip
    Replies:
    0
    Views:
    133
    Phlip
    Jan 15, 2008
Loading...

Share This Page