TK421 TK421 - 3 months ago 42
PHP Question

Parsing xml response from ebay getsellerlist with php

I am trying to parse XML with PHP. The XML is a response from ebay getsellerlist api, and is structured like so:

<!--?xml version="1.0" encoding="UTF-8"?-->
<getsellerlistresponse xmlns="urn:ebay:apis:eBLBaseComponents">
<timestamp>2016-08-11T14:17:39.869Z</timestamp>
<ack>Success</ack>
<version>967</version>
<build>E967_CORE_APISELLING_17965876_R1</build>
<itemarray>
<item>
<itemid>itemid1</itemid>
<listingdetails>
<viewitemurl>itemurl1</viewitemurl>
</listingdetails>
<primarycategory>
<categoryid>categoryid1</categoryid>
<categoryname>categoryname1</categoryname>
</primarycategory>
<title>title1</title>
<picturedetails>
<galleryurl>url1</galleryurl>
<photodisplay>thumbnail1</pictureurl>
<pictureurl>picture1</pictureurl>
</picturedetails>
</item>
</itemarray>
</getsellerlistresponse>


My php is as follows:

<?
$xml = '<!--?xml version="1.0" encoding="UTF-8"?--><getsellerlistresponse xmlns="urn:ebay:apis:eBLBaseComponents"><timestamp>2016-08-11T14:17:39.869Z</timestamp><ack>Success</ack><version>967</version><build>E967_CORE_APISELLING_17965876_R1</build><itemarray><item><itemid>itemid1</itemid><listingdetails><viewitemurl>itemurl1</viewitemurl></listingdetails><primarycategory><categoryid>categoryid1</categoryid><categoryname>categoryname1</categoryname></primarycategory><title>title1</title><picturedetails><galleryurl>url1</galleryurl><photodisplay>thumbnail1</pictureurl><pictureurl>picture1</pictureurl></picturedetails></item><item><itemid>itemid2</itemid><listingdetails><viewitemurl>itemurl2</viewitemurl></listingdetails><primarycategory><categoryid>categoryid2</categoryid><categoryname>categoryname2</categoryname></primarycategory><title>title1</title><picturedetails><galleryurl>url2</galleryurl><photodisplay>thumbnail2</pictureurl><pictureurl>picture2</pictureurl></picturedetails></item></itemarray></getsellerlistresponse>';
$dom = new DOMDocument();
$dom->loadXML($xml);
$title_nodes = $dom->getElementsByTagName('title');

$titles = array();

foreach ($title_nodes as $node) {
$titles[] = $node->nodeValue;
echo $node->nodeValue;
}
echo $titles[0];
echo count($titles);
?>


When I run it, I get a blank page, no errors, nothing.
If I check $titles length using count(), it comes back as zero.
For some reason it is not getting the title node (or any other nodes) and I can't figure out how to parse the xml string with php and get the node values.

Any help most appreciated, if the question is vague or lacking detail, please let me know and I will correct it.

Answer

The XML isn't valid:

Unable to parse any XML input. org.jdom2.input.JDOMParseException: Error on line 2: The element type "photodisplay" must be terminated by the matching end-tag "".

And that's only after you remove the comments in your XML declaration:

<!--?xml version="1.0" encoding="UTF-8"?-->

shoud be

<?xml version="1.0" encoding="UTF-8"?>

Working demo:

<?php

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<getsellerlistresponse xmlns="urn:ebay:apis:eBLBaseComponents">
    <timestamp>2016-08-11T14:17:39.869Z</timestamp>
    <ack>Success</ack>
    <version>967</version>
    <build>E967_CORE_APISELLING_17965876_R1</build>
        <itemarray>
            <item>
                <itemid>itemid1</itemid>
                <listingdetails>
                    <viewitemurl>itemurl1</viewitemurl>
                </listingdetails>
                <primarycategory>
                <categoryid>categoryid1</categoryid>
                <categoryname>categoryname1</categoryname>
            </primarycategory>
            <title>title1</title>
            <picturedetails>
                <galleryurl>url1</galleryurl>
                <photodisplay>thumbnail1</photodisplay>
                <pictureurl>picture1</pictureurl>
            </picturedetails>
        </item>
    </itemarray>
</getsellerlistresponse>';
    $dom = new DOMDocument();
    $dom->loadXML($xml);
    $title_nodes = $dom->getElementsByTagName('title');

    $titles = array();

    foreach ($title_nodes as $node) {
        $titles[] = $node->nodeValue;
        echo $node->nodeValue;
    }
    echo $titles[0];
    echo count($titles);
Comments