Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

LibXML: Can't get value of attribute using DOM

by timtowtdi (Sexton)
on Nov 10, 2016 at 21:35 UTC ( [id://1175700]=perlquestion: print w/replies, xml ) Need Help??

timtowtdi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have an XML-file 'imgfile.xml' and I would like to get the values of Hight, Width and Description. I am basically capable of getting all values of all tags, but not when they are formed like in this XML-file:
<?xml version="1.0" encoding="UTF-16" standalone="yes"?> <imgfile IMGFileVersion="1.2.0.2312"> <images> <views> <view type="contacts_view"> <nodes> <node> <properties> <property type="Hight">434</property> <property type="Width">2346</property> <property type="Description">Smile</proper +ty> </properties> </node> <node> <properties> <property type="Hight">1024</property> <property type="Width">768</property> <property type="Description">Background.jp +eg</property> </properties> </node> </nodes> </view> </views> </images> </imgfile>
Script for parsing:
#!/usr/bin/perl use 5.010; use strict; use warnings; use XML::LibXML; #my $FILENAME = ('imgfile.xml'); my $parser = XML::LibXML->new; my $dom = XML::LibXML->load_xml( location => 'imgfile.xml' ); print 'XML Version is: ', $dom->version, "\n"; print 'Document encoding is: ', $dom->encoding, "\n"; foreach my $image ($dom->findnodes('imgfile/images/views/view/nodes/no +de/properties')) { say 'Description: ', $image->findvalue('./Description'); } exit(0);
I am trying to parse it with the perlscript above, but as you may probably understand, it only returns: (For the example I only try to get the Description-value)
XML Version is: 1.0 Document encoding is: UTF-16 Description: Description:
How can I get the value of the Description property?
The great mistake is to anticipate the outcome of the engagement; Let nature take its course, and your tools will strike at the right moment.

Replies are listed 'Best First'.
Re: LibXML: Can't get value of attribute using DOM
by choroba (Cardinal) on Nov 10, 2016 at 21:43 UTC
    Use @ before attribute names, also, you can't skip property , and you need to use quotes for values. Values, attributes, and elements are three different notions.
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use XML::LibXML; my $dom = 'XML::LibXML'->load_xml(location => 'imgfile.xml'); say 'XML Version is: ', $dom->version; say 'Document encoding is: ', $dom->encoding; for my $image ( $dom->findnodes('imgfile/images/views/view/nodes/node/properties') ) { for my $property (qw( Description Hight Width )) { # Height? say "$property: ", $image->findvalue("property[\@type='$proper +ty']"); } }

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Yes height :-) Thank you! Really happy that it works now.
      The great mistake is to anticipate the outcome of the engagement; Let nature take its course, and your tools will strike at the right moment.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-20 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found