Hi,
Thanks for your help.
For your information (or future readers), I had to take two xml files, add attributes to them xml and join them.

For example :

File1 had:

<?xml version="1.0" encoding="UTF-8"?> <arbre> <branche name="courbe" > <description> <![CDATA[une belle branche]]> </description> <feuilles> <fleur color="blue" order="1" /> <fleur color="white" order="2" /> <fleur color="yellow" order="3" /> </feuilles> </branche> <!--etc...--> </arbre>

File2 had :

<?xml version="1.0" encoding="UTF-8"?> <arbre> <branche name="droite" > <description> <![CDATA[une branche commune]]> </description> <feuilles> <fleur color="purple" order="1" /> <fleur color="green" order="2" /> </feuilles> </branche> <!--etc...--> </arbre>

And the expected result was

<arbre> <branche name="courbe" type="conifere"> <description> <![CDATA[une belle branche]]> </description> <feuilles> <fleur color="blue" order="1" /> <fleur color="white" order="2" /> <fleur color="yellow" order="3" /> </feuilles> </branche> <!--etc...--> <branche name="droite" type="resineux"> <description> <![CDATA[une branche commune]]> </description> <feuilles> <fleur color="purple" order="1" /> <fleur color="green" order="2" /> </feuilles> </branche> <!--etc...--> </arbre>

Here is the code :

sub _mergeXML { my $FILE1 = shift; my $FILE2 = shift; my $FILEOUT = shift; my $parser = XML::LibXML->new(); my @wanted; my $genericDom = $parser->load_xml(location => $FILE1); for my $branch_node ($genericDom->findnodes("/arbre/branche")) { $branch_node->setAttribute(type => "conifere"); } push(@wanted, $genericDom->documentElement->findnodes("./*")); my $customDom = $parser->load_xml(location => $FILE2); for my $branch_node ($customDom->findnodes("/arbre/branche")) { $branch_node->setAttribute(type => "resineux"); } push(@wanted, $customDom->documentElement->findnodes("./*")); my $new = XML::LibXML::Document->new( '1.0', 'UTF-8' ); $new->addChild($new->createComment("So, in the end we have a fores +t...")); my $root = XML::LibXML::Element->new( 'arbre' ); $new->addChild( $root ); $root->addChild( $_ ) for @wanted; my $pp = XML::LibXML::PrettyPrint->new(indent_string => " "); $pp->pretty_print($new); open(FILE,">$FILEOUT") or die $!; print FILE $new->toString;close +FILE; }

Cheers


In reply to Re: Add attribute to xml with XML::LibXML by Yshyeeni
in thread Add attribute to xml with XML::LibXML by Yshyeeni

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.