what i'm trying to do is take a little snippet of text, and insert it into an XML parse tree. the trick is that i want to insert it as XML, not as plain text.

eg, say i have the following XMLish text:

<p>this is a paragraph with some <abbrev>XML</abbrev> in it</p>

i want to be able to stick that into an arbitrary point in another XML document that i've already got parsed into a DOM tree. i don't need to do anything with the tree after i've inserted the text except write it out to a file so it doesn't bother me to have the newly inserted text not parsed into nodes. currently, i'm using XML::DOM but i'm not too attached to it. i've tried creating a new node in the document and using $newnode->setNodeValue($text), then inserting that node into the proper spot in the tree. this sort of works, except it tries to be a little too helpful and escapes the text, turning it into:

&lt;p>this is a paragraph with some &lt;abbrev>XML&lt;/abbrev> in it&lt;/p>

not quite what i want. i've also tried parsing the text into its own tree and just inserting the root node from that tree into the other document's tree but it complains that the node comes from a different document and won't let me do it. the only things i can think of to do now are: 1) do it that way, then run a regexp over the XML file after it's been toString'd unescaping the text (very ugly and not very robust), or 2) parsing the text into its own tree and recursively copying node values from one tree to the other. there has to be a better way.

so can anyone think of a way to either tell XML::DOM not to escape the text, or to otherwise insert a chunk of XML into a tree without nasty recursive traversals?

anders pearson // digital samurai
personal      // http://www.columbia.edu/~anders/
weblog       // http://thraxil.dhs.org/

In reply to inserting text into an XML tree by thraxil

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.