Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Add attribute to xml with XML::LibXML

by Yshyeeni (Initiate)
on Jul 31, 2013 at 13:24 UTC ( [id://1047266]=note: print w/replies, xml ) Need Help??


in reply to Add attribute to xml with XML::LibXML

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

Replies are listed 'Best First'.
Re^2: Add attribute to xml with XML::LibXML
by Yshyeeni (Initiate) on Jul 31, 2013 at 14:34 UTC
    Nop... Nothing to say.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1047266]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found