For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > January 2006 > XML::Simple question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author XML::Simple question
jack

2006-01-21, 3:56 am

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

Bart Van der Donck

2006-01-21, 7:57 am

jack wrote:

> 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];

--
Bart

Mark Clements

2006-01-21, 6:57 pm

Bart Van der Donck wrote:
> jack wrote:
>
>
> 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
Tad McClellan

2006-01-21, 6:57 pm

jack <virtualspy3@gmail.com> wrote:

> 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" }
}
}
---------------


But I kinda doubt the usefulness of the above...


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
jack

2006-01-30, 6:59 pm

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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com