Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Reading XML LibXML

by banzai (Initiate)
on Oct 02, 2013 at 09:24 UTC ( #1056590=perlquestion: print w/ replies, xml ) Need Help??
banzai has asked for the wisdom of the Perl Monks concerning the following question:

okay so my XML looks like this:
<table> <Entry No="1"> <lastname_>Dasd</lastname_> <firstname_>Dasd</firstname_> <street_>Dasd</street_> <houseno_>12</houseno_> <zip_>1231</zip_> <city_>Dasd</city_> <sex_>maennlich</sex_> <marital_>geschieden</marital_> <character_>Brille Sommersprossen</character_> <description_>asdas </description_> </Entry> <Entry No="2"> <lastname_>Dasd</lastname_> <firstname_>Dasd</firstname_> <street_>Dasd</street_> <houseno_>12</houseno_> <zip_>1231</zip_> <city_>Dasds</city_> <sex_>maennlich</sex_> <marital_>geschieden</marital_> <character_>Brille Sommersprossen</character_> <description_>asdas </description_> </Entry> </table>
what i want to do is get the textcontent of the childnotes of the "Entry" nodes and put them in a hash (e.g. $info{"lastname_no of entry, $info{"firstname_no of entry, etc) but i cannot for the life of me do it. what i have so far:
my $file = 'xml.xml'; my $parser = XML::LibXML->new(); my $tree = $parser->parse_file($file); my @tmpinfo; foreach my $sample ($tree->findnodes('/table/Entry')) { $i=0; foreach my $content ($sample->findnodes('*')) { $tmpinfo[$i]=$content->textContent; $i++; print $tmpinfo[$i]; } $i++; }
i would put the content of $tmpinfo into the hash later, but it tells me $tmpinfo is uninitialized? im so completely burnt out on this problem, please help.

Comment on Reading XML LibXML
Select or Download Code
Re: Reading XML LibXML (ddumper)
by Anonymous Monk on Oct 02, 2013 at 09:31 UTC

    i would put the content of $tmpinfo into the hash later, but it tells me $tmpinfo is uninitialized?

    Maybe you want \@tmpinfo ?

    use Basic debugging checklist item 4 ( Dumper ) to ddumperBasic debugging checklist up \@tmpinfo before/inside/after the LOOP

    Works for me :)

    #!/usr/bin/perl -- use strict; use warnings; use XML::LibXML 1.70; ## for load_html/load_xml/location use Data::Dump; my $xml=q{<table> <Entry No="1"> <lastname_>Dasd</lastname_> <firstname_>Dasd</firstname_> <street_>Dasd</street_> <houseno_>12</houseno_> <zip_>1231</zip_> <city_>Dasd</city_> <sex_>maennlich</sex_> <marital_>geschieden</marital_> <character_>Brille Sommersprossen</character_> <description_>asdas </description_> </Entry> <Entry No="2"> <lastname_>Dasd</lastname_> <firstname_>Dasd</firstname_> <street_>Dasd</street_> <houseno_>12</houseno_> <zip_>1231</zip_> <city_>Dasds</city_> <sex_>maennlich</sex_> <marital_>geschieden</marital_> <character_>Brille Sommersprossen</character_> <description_>asdas </description_> </Entry> </table> }; my $tree = XML::LibXML->new( qw/ recover 2 / )->load_xml( string => $x +ml ); my @nodeboks; for my $sample ( $tree->findnodes('/table/Entry') ){ for my $content ( $sample->findnodes('*') ){ print $content->nodePath, "\n"; push @nodeboks, $content->textContent; } } dd\@nodeboks; __END__
Re: Reading XML LibXML
by Bloodnok (Vicar) on Oct 02, 2013 at 10:15 UTC
    Hmmm, so why not let XML::Simple take the strain ?

    A user level that continues to overstate my experience :-))

      Hmmm, so why not let XML::Simple take the strain ?

      Because that isn't the question; it can't handle it :)

      The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended.

      The major problems with this module are the large number of options and the arbitrary ways in which these options interact - often with unexpected results.

        use strict; use warnings; use XML::Simple; my $xml = <<XML; <table> <Entry No="1"> <lastname_>Dasd</lastname_> <firstname_>Dasd</firstname_> <street_>Dasd</street_> <houseno_>12</houseno_> <zip_>1231</zip_> <city_>Dasd</city_> <sex_>maennlich</sex_> <marital_>geschieden</marital_> <character_>Brille Sommersprossen</character_> <description_>asdas </description_> </Entry> <Entry No="2"> <lastname_>Dasd</lastname_> <firstname_>Dasd</firstname_> <street_>Dasd</street_> <houseno_>12</houseno_> <zip_>1231</zip_> <city_>Dasds</city_> <sex_>maennlich</sex_> <marital_>geschieden</marital_> <character_>Brille Sommersprossen</character_> <description_>asdas </description_> </Entry> </table> XML my $data = XMLin( $xml ); my @info = @{$data->{'Entry'}}; for my $entry (@info){ print "$entry->{lastname_}, $entry->{firstname_}\n"; }
Re: Reading XML LibXML
by GotToBTru (Chaplain) on Oct 02, 2013 at 13:10 UTC
    The reason for the uninitialized message is you are incrementing $i between the assignment to $tmpinfo[$i] and printing $tmpinfo[$i].
Re: Reading XML LibXML
by Anonymous Monk on Oct 02, 2013 at 13:14 UTC
    Also ... an XSLT stylesheet (having nothing to do with CSS, etc ...) can dig through an XML structure and handle tasks like this without programming. If you find that you are going to be "writing a lot of little programs" to do this task, they might be a more comprehensive solution.

      First, writing XSLT IS PROGRAMMING. No matter what the marketing guys say it's just as much programming as, say, writing SQL is.

      Second, how's xml in xml in xml in xml that xml the xml to xml the xml xmling until xmlly xmled gonna help in converting the file into a data structure to be used in a program? Do you plan to write a stylesheeet that'd convert he xml into a piece of Perl code you can eval""? Or ... in case the program was supposed to import the data into a database, convert it to a bunch of SQL statements?

      This more comprehensive solution is, more often than not, both next to impossible to read and, as soon as you happen to need anything but the very basic transformations, fragile and XSLT processor dependent. Even if both the source and the destination is XML, XSLT gives you little more than just the ability to change the structure. How much can you do with the tag contents or the values of the attributes? How do you fetch data from other sources?

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

Re: Reading XML LibXML
by Jenda (Abbot) on Oct 02, 2013 at 15:44 UTC

    You want XML::Rules. With a few simple rules it converts the XML into a data structure. Though $info->{$no_of_entry}{lastname} would be both easier to get and use than $info{"lastname"}[$no_of_entry]

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056590]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-09-02 05:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (20 votes), past polls