I want to parse a simple string using XML::Parser. This works fine. But I do not know how to access the result.
#!/usr/bin/perl
use Data::Dumper;
use XML::Parser;
my $parser = XML::Parser->new( Style => 'Tree' );
my $tree = $parser->parse( '<xml type="test" Id="ca19cfd5" result="1 test 2 test 3" elapsed="9" Size="12345" />' );
print Dumper( $tree );
shows me
$VAR1 = [
'xml',
[
{
'Size' => '12345',
'Id' => 'ca19cfd5',
'type' => 'test',
'elapsed' => '9',
'result' => '1 test 2 test 3'
}
]
];
So it perfectly could parse my string. But how do I access these fields? Something like "my $result = $tree..."
The given xml string will always have the same syntax like shown above. Only the content differs.
Tnx in advance, Enkidu
The attributes you are looking for are in $tree->[1]->[0]
my $atts= $tree->[1]->[0];
then
print "size: $atts->{Size}\n";
That said XML::Parser is not the easiest module to use, it's a bit too low-level. XML::Twig or XML::LibXML will give you what you want more easily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With