Hi Monks,

Once again I am fighting with XML. I would like to get values set for 'value': where the 'field name="Name"' and 'class="FILENAME' and where 'field name="Size"'. (independent the class)

The (simplified) version of the XML file is as follows:

<?xml version="1.0" encoding="UTF-16" standalone="yes"?> <files> <xml version="12.0.0.1" /> <file name="Image"> <item> <field name="Name" value="Smile.JPG" class="FILENAME"/> <field name="Compression" value="0" class="INFO"/> <field name="Type" value="OS" class="INFO"/> <field name="Size" value="4120893467kB" class="RAM"/> </item> <item> <field name="Name" value="WALLPAPER.JPG" class="FILENAME"/> <field name="Compression" value="1" class="INFO"/> <field name="Type" value="OS" class="INFO"/> <field name="Size" value="2MB" class="DISK"/> </item> </file> </files>

This is what I have so far:

#!/usr/bin/env 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; # RETURNS ONLY WHERE name="Name" IN ONE ITEM for my $fileline ( $dom->findnodes('/files/file[@name="Image"]/item/fi +eld[@name="Name"]') ) { say 'Name : ', $fileline->getAttribute('value'); } # RETURNS ALL VALUES IN ONE ITEM for my $fileline ( $dom->findnodes('/files/file[@name="Image"]/item/fi +eld') ) { say 'Value: ', $fileline->getAttribute('value'); } # NOT WORKING for my $fileline ( $dom->findnodes('/files/file[@name="Image"]/item') +) { say 'Name : ', $fileline->getAttribute('field[@name="Name"]/value' +); say 'Size : ', $fileline->getAttribute('field[@name="Size"]/value' +); }

Anyone can help?

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.

In reply to LibXML: Can't get value by timtowtdi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.