I've been trying to write some code to read and write some xml and having no luck. I'm attempting to use XML::XPath, but I'm open to others if anyone has any suggestions. (I tried XML::Simple, but it completely butchers the document in the output.)

Here's some sample xml:

<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE ADI SYSTEM "ADI.DTD"> <ADI> <Metadata> <AMS Asset_Class="package" Asset_Name="xxxxxxxxxxxxxxxxxxxxxxxx_pa +ckage" /> <App_Data App="MOD" Name="Metadata_Spec_Version" Value="xxxxxxxxxx +xxxxx"/> <App_Data App="MOD" Name="Metadata_Tier" Value="xxxxxxxxxxxxxxxxxx +xxxxx"/> </Metadata> <Asset> <Metadata> <AMS Asset_Class="title" Asset_Name="xxxxxxxxxxxxxxxxxxxxxxxx_ti +tle" /> <App_Data App="MOD" Name="Type" Value="title" /> <App_Data App="MOD" Name="Year" Value="1999" /> </Metadata> <Asset> <Metadata> <AMS Asset_Class="poster" Asset_Name="xxxxxxxxxxxxxxxxxxxxxxxx +_poster" /> <App_Data App="MOD" Name="Content_CheckSum" Value="3167abc898d +ba878794754dc4afca0fd"/> <App_Data App="MOD" Name="Content_FileSize" Value="230454"/> <App_Data App="MOD" Name="Image_Aspect_Ratio" Value="320x240"/ +> <App_Data App="MOD" Name="Type" Value="poster"/> </Metadata> <Content Value="xxxxxxxxxxxxxxxx.bmp" /> </Asset> <Asset> <Metadata> <AMS Asset_Class="movie" Asset_Name="xxxxxxxxxxxxxxxxxxxxxxxx_ +movie" /> <App_Data App="MOD" Name="Setting1" Value="Y"/> <App_Data App="MOD" Name="Languages" Value="en"/> </Metadata> <Content Value="xxxxxxxxxxxxxxxxxxxxxxxxxx.mpg"/> </Asset> </Asset> </ADI>

So, for some example code:

#!/usr/bin/perl use strict; use warnings; use XML::XPath; use Data::Dumper; my $xpath = XML::XPath->new(filename => 'TEST.XML'); my $nodeset = $xpath->find('/ADI/Asset/Asset/Metadata[AMS[@Asset_Class +="poster"]]'); ddump('ref nodeset', __LINE__, ref $nodeset); # this shows 'XML::XPat +h::NodeSet'

After this, I've tried iterating with $nodeset->get_nodelist and then iterating those nodes with the ->getChildNodes and using ->string_value and ->getName methods, but I can never seem to get at what I want. I've tried a variety of Xpath strings, notably .//AMS[@Asset_Class="poster" which with an xpath tester does get me to where I expect, I just can't seem to turn that into something I can code against.

Any help appreciated. Tearing my hair out for way too long on this. Even tried AI to at least look for pointers. No, it didn't work at all. #facepalm

(ddump() is just my little wrapper around Data::Dumper, it's only slightly more advanced than print Dumper().)

--
Andy


In reply to XML Parsing and Xpath confusion by naChoZ

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.