Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Using this code I am able to print out the data listed WITHIN the <document> tag. :<?xml version="1.0" encoding="UTF-8"?> <results> <document id="\2006\200601\20060125\20060125_18.txt" datetime="2006/01/25" sourcecategory="News Archive" schemeversion="1.1"> </document> <document id="\2006\200601\20060125\20060125_19.txt" datetime="2006/01/25" sourcecategory="News Archive" schemeversion="1.1"> <record> <sentence-number>3</sentence-number> <data-class>Target</data-class> <group>P6</group> </record> <record> <sentence-number>12</sentence-number> <data-class>Good</data-class> <group>P6</group> </record> </document> </results>,
I get something like: 2006/01/25,\2006\200601\20060125\20060125_18.txt,News Archive,1.1 which is perfect. but I have NO IDEA how to get at the second level of data held inside the <record> tags. Some of the <document>s have them and some don't, and the ones that do have <record> tags could have any number of them (1, 2, 7, 14, etc.). Can anyone help me access them? I'd like to print a line for each <record> that also lists the information contained in the corresponding <document>. In theory I'd like it to look like this:!/usr/bin/perl # open an output file unless (open (OUTFILE, ">testoutput.xml")){ die ("Cannot open output file testoutput.xml\n"); } use XML::Simple; my $file = "infile.xml"; my $xs1 = XML::Simple->new(); my $doc = $xs1->XMLin($file); foreach my $key (keys (%{$doc->{document}})){ print $doc->{document}->{$key}->{'datetime'}, ",", '(' . $key . ')', ",", $doc->{document}->{$key}->{sourcecategory}, ",", $doc->{document}->{$key}->{schemeversion}, ",", "\n"; }
I hope someone can help! Thanks, Hans2006/01/25,\2006\200601\20060125\20060125_18.txt,News Archive,1.1 2006/01/25,\2006\200601\20060125\20060125_19.txt,News Archive,1.1,3,Ta +rget,P6 2006/01/25,\2006\200601\20060125\20060125_19.txt,News Archive,1.1,12,G +ood,P6
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML parsing question
by ikegami (Patriarch) on Sep 01, 2009 at 03:23 UTC | |
|
Re: XML parsing question
by toolic (Bishop) on Sep 01, 2009 at 03:22 UTC | |
|
Re: XML parsing question
by astroboy (Chaplain) on Sep 01, 2009 at 03:16 UTC | |
|
Re: XML parsing question
by Anonymous Monk on Sep 01, 2009 at 03:23 UTC | |
|
Re: XML parsing question
by Jenda (Abbot) on Sep 03, 2009 at 21:04 UTC |