XML::Simple question

J

jack

Hi all,

I am working on perl..and am using XML::Simple to parse a xml document.
I've been trying to retrieve character data from tags whose occurance
is recursive.. The scenario can be better explained as below:

For a structure like,

<employees>
<employee>
<name>John Doe</name>
<age>43</age>
<sex>M</sex>
<department>Operations</department>
</employee>
</employees>

To access the value "John Doe", we can use the syntax:
$data->{employee}->[0]->{name} {$data is a XML::Simple variable}

But, how do I access the character data between the tags..when the tag
structure of the XML is as shown below..and tagging structure changes
with the document and is not standard, is there a function or
module..or technique..which works recursively..depending on the nested
tagging structure of the XML document and retrieves the character data
located in the lowest level of the nesting?

How do the access all the data from this structure?

<outline>
<item>
<item>Proteins and Phospholipid Measurements</item>
<item>comprehension requirements</item>
</item>
<item>
<item>educational communication.</item>
<item>
<item>traditional classroom.</item>
<item>peers around the world.</item>
</item>
</item>
</outline>

Jack
 
B

Bart Van der Donck

jack said:
I am working on perl..and am using XML::Simple to parse a xml document.
[...]
How do the access all the data from this structure?

<outline>
<item>
<item>Proteins and Phospholipid Measurements</item>
<item>comprehension requirements</item>
</item>
<item>
<item>educational communication.</item>
<item>
<item>traditional classroom.</item>
<item>peers around the world.</item>
</item>
</item>
</outline>

Use double or triple references:

#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;

my $ref = XMLin("
<outline>
<item>
<item>Proteins and Phospholipid Measurements</item>
<item>comprehension requirements</item>
</item>
<item>
<item>educational communication.</item>
<item>
<item>traditional classroom.</item>
<item>peers around the world.</item>
</item>
</item>
</outline>
");

# this prints "comprehension requirements"
print $ref->{item}->[0]->{item}->[1]."\n";

# this prints "traditional classroom."
print $ref->{item}->[1]->{item}->[1]->{item}->[0];
 
M

Mark Clements

Bart said:
jack said:
I am working on perl..and am using XML::Simple to parse a xml document.
[...]
How do the access all the data from this structure?

<outline>
<item>
<item>Proteins and Phospholipid Measurements</item>
<item>comprehension requirements</item>
</item>
<item>
<item>educational communication.</item>
<item>
<item>traditional classroom.</item>
<item>peers around the world.</item>
</item>
</item>
</outline>

Use double or triple references:

#!/usr/bin/perl
use strict;
use warnings;
use XML::Simple;

my $ref = XMLin("
<outline>
<item>
<item>Proteins and Phospholipid Measurements</item>
<item>comprehension requirements</item>
</item>
<item>
<item>educational communication.</item>
<item>
<item>traditional classroom.</item>
<item>peers around the world.</item>
</item>
</item>
</outline>
");

# this prints "comprehension requirements"
print $ref->{item}->[0]->{item}->[1]."\n";

# this prints "traditional classroom."
print $ref->{item}->[1]->{item}->[1]->{item}->[0];

I think he meant nesting of an arbitrary depth (though, to be fair, this
isn't very clear). I'd probably use XML::XPath myself.

Mark
 
T

Tad McClellan

jack said:
I am working on perl..and am using XML::Simple to parse a xml document.
I've been trying to retrieve character data from tags whose occurance
is recursive..


But that isn't actually "simple". You may need a heavier weight module.

But, how do I access the character data between the tags..when the tag
structure of the XML is as shown below..and tagging structure changes
with the document and is not standard, is there a function or
module..or technique..which works recursively..depending on the nested
tagging structure of the XML document and retrieves the character data
located in the lowest level of the nesting?


---------------
#!/usr/bin/perl
use warnings;
use strict;
use XML::Simple;
use Data::Dumper;

my $xml =<<ENDXML;
<outline>
<item>
<item>Proteins and Phospholipid Measurements</item>
<item>comprehension requirements</item>
</item>
<item>
<item>educational communication.</item>
<item>
<item>traditional classroom.</item>
<item>peers around the world.</item>
</item>
</item>
</outline>
ENDXML

my $ref = XMLin $xml;
print Dumper $ref;
recurse_simple( $ref );

sub recurse_simple {
foreach ( @_ ) {
if ( ref $_ eq 'HASH' ) { recurse_simple( values %$_ ) }
elsif ( ref $_ eq 'ARRAY' ) { recurse_simple( @$_ ) }
else { print "$_\n" }
}
}
 
J

jack

Hi Tad,

Thanks for your example. It kind of server the purpose for me. But, How
do I differentiate the levels of item?
Should I send more than one variable in the recurse_simple function? I
need to display the character data between the item tags and also
specify the level of that particular data. Please let me know how I can
do it?

Jack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top