naChoZ has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML Parsing and Xpath confusion
by ikegami (Patriarch) on Jul 03, 2024 at 14:09 UTC | |
by naChoZ (Curate) on Jul 08, 2024 at 14:16 UTC | |
|
Re: XML Parsing and Xpath confusion
by sectokia (Friar) on Jul 04, 2024 at 02:00 UTC | |
by ikegami (Patriarch) on Jul 04, 2024 at 15:18 UTC |