3

I have an XML file like so:

<?xml version="1.0" encoding="UTF-8"?>
<GeteBayDetailsResponse xmlns="urn:ebay:apis:eBLBaseComponents">
  <Timestamp>2012-07-04T13:54:51.873Z</Timestamp>
  <Ack>Success</Ack>
  <Version>779</Version>
  <Build>E779_CORE_BUNDLED_14986049_R1</Build>
  <SiteDetails>
    <Site>US</Site>
    <SiteID>0</SiteID>
    <DetailVersion>1</DetailVersion>
    <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
  </SiteDetails>
  <SiteDetails>
    <Site>Canada</Site>
    <SiteID>2</SiteID>
    <DetailVersion>1</DetailVersion>
    <UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
  </SiteDetails>
</GeteBayDetailsResponse>

I am trying to find a way to get the value of the SiteID node where the Site node equals whatever.

I have tried using XPath and DomDocument functions but without any luck. Some of the messed up code is below:

    $xml_file ='SiteDetails.xml';
    $xmlDoc = new DomDocument();
    $xmlDoc->load($xml_file);
    $xpath = new DOMXpath($xmlDoc);

    $xpath->registerNamespace('ebay', 'urn:ebay:apis:eBLBaseComponents');

    //$siteIDList = $xpath->query("/ebay:GeteBayDetailsResponse/ebay:SiteDetails[ebay:Site='UK']/ebay:SiteID");
    //$siteIDList =  $xpath->query("/GeteBayDetailsResponse/SiteDetails[Site=\"UK\"]/SiteID");

//$siteIDList =  $xpath->query("//*[namespace-uri()='urn:ebay:apis:eBLBaseComponents' and name()='Site' and text()='$site']/following-sibling::*[namespace-uri()='urn:ebay:apis:eBLBaseComponents' and name()='SiteID']/text()");

    //var_dump($siteIDList);
    //echo $siteIDList->item(0)->nodeValue;

            /*$SiteDetails = $xmlDoc->getElementsByTagName('SiteDetails');
            $count = 0;
            foreach($SiteDetails as $details){
            //  print_r($details);
            echo $details->nodeValue;
                if ($details->nodeValue == $site){
                    echo 'YEAH';
                    echo $details->SiteID;
                }
            $count ++;  
            }*/
            //$siteID = $siteIDNode->item(8)->nodeValue;        

    //var_dump($siteIDNode);
}

Can anyone help? As you can see from the above, I am confused...

2
  • Can you clarify: You want to get SiteID from a specific SiteDetails node where Site node equals X? For example given "Canada" you want to get "2"? Commented Jul 5, 2012 at 10:32
  • yeah thats it exactly. thanks Commented Jul 5, 2012 at 10:49

1 Answer 1

3

The general idea is that you iterate over each SiteDetails node and check the Site node value against the value you are looking for.

Assuming $xml contains your xml as SimpleXML instance

$site = 'Canada';
$siteId = null;

foreach($xml->SiteDetails as $sd) {
  if(((string)$sd->Site) === $site) {
    $siteId = (int)$sd->SiteID;
    //break;
  }
}

var_dump($siteId); //int(2)

Here is a working example on codepad

Solution using xpath()

Here is a solution using xpath()

$xml->registerXPathNamespace('ebay','urn:ebay:apis:eBLBaseComponents');
$site = 'Canada';
$siteId = $xml->xpath('ebay:SiteDetails/ebay:Site[text()="'.$site.'"]/../ebay:SiteID');
echo (string)$siteId[0]; //2

working example in codepad

Explanation:

  1. First we find SiteDetails node with child node Site with value "Canada" (or any other value in $site) by using text() function of xpath which returns the value of the element.
  2. Then we go to the corresponding father SiteDetails by using .. xpath selector.
  3. Lastly we extract the value of SiteID from the needed SiteDetails element.

Good luck =)

Sign up to request clarification or add additional context in comments.

2 Comments

Ive tried to go with the 2nd option usin Xpath and I get "Cannot use object of type DOMNodeList as array" error, when I do a var_dump I get an empty object. THis is the same result as I got from my code above. I will try the first option.
@LeeTee Try to use SimpleXML instead of DOMNodeList, this should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.