thraxil has asked for the wisdom of the Perl Monks concerning the following question:

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/

Replies are listed 'Best First'.
Re: inserting text into an XML tree
by mirod (Canon) on May 11, 2001 at 09:17 UTC

    You can do this in XML::Twig by creating the element with the parse method:

    #!/bin/perl -w use strict; use XML::Twig; # create and load the twig (with pretty_print option) my $twig= new XML::Twig( pretty_print => 'indented'); $twig->parse( \*DATA); # get the insertion spot, you could also use the get_xpath method my $insert= $twig->root->first_child( 'insert'); # parse the text into a twig element my $text="<p>this is a paragraph with some <abbrev>XML</abbrev> in it< +/p>"; my $elt= parse XML::Twig::Elt( $text); # replace the <insert> element by the new one, you # can use $elt->insert to... insert the element $elt->replace( $insert); # out we go! $twig->print; print "\n"; __DATA__ <?xml version="1.0"?> <doc> <elt>just a filler</elt> <insert /> <elt>just another filler</elt> </doc>

    You can go to the XML::Twig page for more info, including a new Quick Ref page to sort out the important from the rest in the doc ;--)

Re: inserting text into an XML tree
by Banky (Acolyte) on May 11, 2001 at 08:40 UTC
    Kind of a kludge but one may might be to wrap the text in a unique root node, parse it and then insert all of the nodes/subtrees under your root node into the DOM to be printed.