Hi All,

Forgive me if I use improper terminology or just make a fool of myself with these code examples.

I have two xml documents, one is called input.xml and the other cde.xml. input.xml simply contains a tree that I wan't to add / merge into cde.xml

Here is the code that I have so far which does not work. I am not even sure that what I am trying to do is possible at this point. I may have to just write code to add these elements to cde.xml without copy or paste.

#!/usr/bin/perl -w # use strict; use warnings; use XML::Twig; my $input = XML::Twig->new( twig_roots => { Questions => \&comments }, ); $input->parsefile( 'input.xml'); my $cde = XML::Twig->new( twig_roots => { QQ_Manifest => \&questions } +, twig_print_outside_roots => 1, keep_spaces => 1, ); $cde->parsefile( 'cde.xml'); $cde->flush; sub questions { my( $cde, $questions) = @_; my $question = $questions->first_child( 'Questions'); foreach ( @comments ) { $_->paste( last_child => $question); } } sub comments { my( $input, $comments) = @_; my $comment = $comments->first_child( 'Comments'); push @comments, $comment; }

XML Example:

input.xml
<Questions> <Comments number="12"><Prompt><![CDATA[Question placeho +lder for missing question in RC]]></Promp t><Response score="1.0"><![CDATA[No answer available]]></Response></Co +mments> <Comments number="13"><Prompt><![CDATA[Question placeho +lder for missing question in RC]]></Promp t><Response score="1.0"><![CDATA[No answer available]]></Response></Co +mments> <Comments number="14"><Prompt><![CDATA[Question placeho +lder for missing question in RC]]></Promp t><Response score="1.0"><![CDATA[No answer available]]></Response></Co +mments> </Questions>

Snippet of cde.xml
..... <QQ_Manifest> <Applicant id="applicant1" person_id="50-8082308" email="jac +k_black@aolsuxor.com"/> <Scoresheet id="scoresheet1" person_id="5080438" req_id="564 +3sa34" score="0.8000dsa001" timespent="0" /> <Questions includes_responses="yes"> <No number="10"> ....

I just want the children of input.xml/Questions added to the children of cde.xml/Questions. The problem is that Comments has children and when I paste it tells me I can't paste a tree.

Hope this makes sense.

In reply to XML::Twig - How to copy tree from one document to another? by dbmathis

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.