我有来自Ebay的以下XML文件
<?xml version="1.0"?>
<GeteBayDetailsResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2012-07-04T12:02:14.541Z</Timestamp>
<Ack>Success</Ack>
<Version>779</Version>
<Build>E779_INTL_BUNDLED_14986004_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>
<SiteDetails>
<Site>UK</Site>
<SiteID>3</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Germany</Site>
<SiteID>77</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Australia</Site>
<SiteID>15</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>France</Site>
<SiteID>71</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>eBayMotors</Site>
<SiteID>100</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Italy</Site>
<SiteID>101</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Netherlands</Site>
<SiteID>146</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Spain</Site>
<SiteID>186</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>India</Site>
<SiteID>203</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>HongKong</Site>
<SiteID>201</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Singapore</Site>
<SiteID>216</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Malaysia</Site>
<SiteID>207</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Philippines</Site>
<SiteID>211</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>CanadaFrench</Site>
<SiteID>210</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Poland</Site>
<SiteID>212</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Belgium_Dutch</Site>
<SiteID>123</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Belgium_French</Site>
<SiteID>23</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Austria</Site>
<SiteID>16</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Switzerland</Site>
<SiteID>193</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<SiteDetails>
<Site>Ireland</Site>
<SiteID>205</SiteID>
<DetailVersion>1</DetailVersion>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</SiteDetails>
<UpdateTime>2009-07-09T10:48:17.000Z</UpdateTime>
</GeteBayDetailsResponse>
我需要获取节点SiteID的值,其中节点Site匹配我传递的任何变量。我的代码是:
$xml_file ='SiteDetails.xml';
$xmlDoc = new DomDocument();
$xmlDoc->load($xml_file);
$xpath = new DOMXpath($xmlDoc);
$siteIDList = $xpath->query("/GeteBayDetailsResponse/SiteDetails[Site=$site]/SiteID");
var_dump($siteIDList);
echo $siteIDList->SiteID;
我得到以下结果:
object(DOMNodeList)#11 (0) {
}
有人可以帮忙吗?我希望得到3的值。
答案 0 :(得分:0)
我认为你必须用引号括住$site
并添加命名空间:
$xpath = new DOMXPath($xmlDoc);
$xpath->registerNamespace('ebay', 'urn:ebay:apis:eBLBaseComponents');
$siteIDList = $xpath->query("/ebay:GeteBayDetailsResponse/ebay:SiteDetails[ebay:Site='$site']/ebay:SiteID");
可以肯定的是,可以给我们$site
这个给你这个结果的值吗?
编辑:按照Vaman Kulkarni的建议添加名称空间。
答案 1 :(得分:0)
修改:来自Florent的回答使用过的API registerNamespace
。
您的XML文件包含默认命名空间。因此,您可以在代码中对默认命名空间(ebay
)注册变量名称urn:ebay:apis:eBLBaseComponents
,如下所示,并使用xpath
$xpath->registerNamespace('ebay', 'urn:ebay:apis:eBLBaseComponents');
$siteIDList = $xpath->query("//ebay:Site[text()='$site']/following-sibling::ebay:SiteID/text()");
或强>
你可以使用以下XPATH,除了评估它之外不需要任何额外的代码: - )
//*[namespace-uri()='urn:ebay:apis:eBLBaseComponents' and name()='Site' and text()='$site']/following-sibling::*[namespace-uri()='urn:ebay:apis:eBLBaseComponents' and name()='SiteID']/text()
注意: $ site是<Site>
节点的运行时值,其值<SiteID>
将匹配。