我有一个像这样的 XML 文件:
<?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>
我正在尝试找到一种方法来获取 SiteID 节点的值,其中 Site 节点等于任何值。
我曾尝试使用 XPath 和 DomDocument 函数,但没有任何运气。一些混乱的代码如下:
$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);
}
任何人都可以帮忙吗?从上面可以看出,我很困惑......