I am attempting to parse an xml file with attributes, and keep getting an empty array. Here is a sample of the xml, which is parsed as a simplexml_load_string:
<NumberOfOfferListings>
<OfferListingCount condition="Any">61</OfferListingCount>
<OfferListingCount condition="Used">45</OfferListingCount>
<OfferListingCount condition="New">16</OfferListingCount>
</NumberOfOfferListings>
Here is the php code that I am using
$priceComp_xml = amazonCompPrice_xml($asin);
$compPricing = $priceComp_xml->xpath('OfferListingCount[@condition="Any"]');
amazonCompPrice($asin) is the parsed xml file based on the ASIN value.
I need to extract just:
<OfferListingCount condition="Any">61</OfferListingCount>
I have looked at many examples on here to get to this point, and it looks like what I have is correct, just returns an empty array when I use either print_r($compPricing) or var_dump.
How do I fix this to get the information that I need??
I can upload any more snippets of code that will help resolve this issue.
There are (at least) two separate issues at play here.
The XPath
Your XPath,
OfferListingCount[@condition="Any"], will only return matching<OfferListingCount>elements which are children of the element held in$priceComp_xml. It will not match descendant elements which are grand-children, or further down the tree.So, it needs to be amended to match the
<OfferListingCount>element(s). A quick fix usually to employ the shorthand//(short for/descendant-or-self::node()/), like//OfferListingCount[@condition="Any"].Namespaces
Your question did not mention this, but some digging uncovered that the XML document probably has a default namespace applied to it. This can be recognised by looking at the document element for
xmlns="…". When using XPath, this namespace needs to be registered and used when querying.Finally, remember that
SimpleXMLElement::xpath()returns an array, so your matching<offerListingCount>element will be available as$compPricing[0].