I currently have a Perl CGI script that parses incoming XML requests using XML::Simple. The convention of the incoming XML is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<XMLRequest>
<CORE>
<CORE ID="14">
<PARENT_ID>1</PARENT_ID>
<CORE_ID>14</CORE_ID>
<FORM_ID>1423</FORM_ID>
<SECURITY_ID>1</SECURITY_ID>
<AREA_ID>3</AREA_ID>
<SUB_PARENT_ID>1</SUB_PARENT_ID>
<SUB_AREA_ID>1</SUB_AREA_ID>
</CORE>
<CORE ID="15">
<PARENT_ID>1</PARENT_ID>
<CORE_ID>15</CORE_ID>
<FORM_ID>1424</FORM_ID>
<SECURITY_ID>1</SECURITY_ID>
<AREA_ID>3</AREA_ID>
<SUB_PARENT_ID>1</SUB_PARENT_ID>
<SUB_AREA_ID>2</SUB_AREA_ID>
</CORE>
</CORE>
</XMLRequest>
I need to get to the name/value pairs and from the convention, the name component is ‘CORE’ and the value being the value of ‘ID’. The challenge though is that ‘CORE’, in this convention represents a changing list of possible values. AS is, XMLin parses that XML so that the topmost and secondary ‘CORE’ are hashes, but then the ‘ID’ and the rest of the data as an array of hashes.
I’ve gone at it from the aspect of using the KeyAttr option of XML::Simple and listing all the possible variations of ‘CORE’ for the values, but this is a changing list so that’s not feasible. The other is just using nested Whiles and For statements to get down to the components needed, but that seems counter-intuitive. I started looking at XML::LibXML but it seems a bit more daunting to jump into.
Suggestions greatly appreciated.
I find
XML::Simpleto be more awkward than simple, and would highly recommend that you progress to XML::LibXML or XML::Twig if only to get access to XPath expressions to access the XML DOM.Here is some code to do what I think you want with your XML. It finds all elements two levels below the root
<XMLRequest>element that have anIDattribute and reports that element’s tag name and itsIDattribute value. As you see it is done in only a couple of lines.output