2012-07-04 51 views
0

獲得價值我有followng XML文件來自eBayPHP的XPath - 如何從結果

<?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在該節點匹配網站我傳進去的變量的值。我的代碼是:

$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

通常對於很長的XML字符串(或者代碼當然),最好在粘貼之前將它很好地嵌套起來,這樣每個人都可以閱讀':)' – halfer

回答

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建議。

+0

這個XPath不會工作,因爲xml包含默認名稱空間。在形成XPath表達式時應考慮命名空間。 –

+0

增加了命名空間。謝謝。 – Florent

0

編輯:來自Florent的回答使用的API registerNamespace

您的XML文件包含默認名稱空間。因此,你可以下面的XPath像ebay對默認命名空間(urn:ebay:apis:eBLBaseComponents)就像在你的代碼的下方,使用註冊一個變量名

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

$siteIDList = $xpath->query("//ebay:Site[text()='$site']/following-sibling::ebay:SiteID/text()");

OR

您可以使用以下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>節點的運行值的<SiteID>值將被匹配。

+0

不知道我明白了,我已經嘗試了您的第一個選項,並獲得了與第一次相同的結果,即。對象(DOMNodeList)#11(0){ }我希望的值不是對象 – LeeTee

+0

這兩個Xpaths都會返回節點''的值,因爲xpath結尾的'/ text()'將匹配值的節點 –

+0

如何訪問節點的值?我錯過了什麼:/ – LeeTee