Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Parse multiple xml tags with different names to an array

by rbala (Acolyte)
on Sep 26, 2013 at 07:13 UTC ( #1055791=note: print w/ replies, xml ) Need Help??


in reply to Re: Parse multiple xml tags with different names to an array
in thread Parse multiple xml tags with different names to an array

The problem is solved. The following is the code:

use XML::LibXML; use XML::Simple; my $xml_simple = new XML::Simple( SuppressEmpty => 1, KeyAttr => "INDEX", ForceArray => 1, KeepRoot => 1, ); my @nodeblocks; my $parser = XML::LibXML->new(); my $doc = $parser->parse_string($string); my @nodes = $doc->findnodes("//ROOT_TAG/*"); foreach my $node(@nodes) { my $str = $node->tostring(); my $str_hash = $xml_simple->XMLin($str); push(@nodeblocks, $str_hash); }


Comment on Re^2: Parse multiple xml tags with different names to an array
Download Code
Re^3: Parse multiple xml tags with different names to an array
by Anonymous Monk on Sep 26, 2013 at 13:19 UTC
    So you're after this?
    my @nodeblocks = ( { CHILD1 => ["ABC"] }, { CHILD2 => ["KJLK"] }, { CHILD3 => ["NLLKJ"] }, );

    #!/usr/bin/perl -- use strict; use warnings; use XML::LibXML 1.70; ## for load_html/load_xml/location use Data::Dump; my $xml=q{<ROOT_TAG> <CHILD1>ABC</CHILD1> <CHILD2>KJLK</CHILD2> <CHILD3>NLLKJ</CHILD3> </ROOT_TAG>}; my $dom = XML::LibXML->new( qw/ recover 2 / )->load_xml( string => $xml ); my @nodeboks; for my $kid ( $dom->findnodes(q{ //ROOT_TAG/* } ) ){ print $kid->nodePath, "\n"; push @nodeboks, { $kid->tagName => [ $kid->textContent ], }; } dd\@nodeboks; __END__ /ROOT_TAG/CHILD1 /ROOT_TAG/CHILD2 /ROOT_TAG/CHILD3 [ { CHILD1 => ["ABC"] }, { CHILD2 => ["KJLK"] }, { CHILD3 => ["NLLKJ"] }, ]

    If your real problem is this short, this approach isn't too bad, but if its more comples, its not a substitute for what XML::Simple does for you, but XML::Rules is, its better than XML::Simple

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-07-10 03:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (198 votes), past polls