2014-12-06 109 views
0

我在使用SimpleXML讀取Spreadshirt API中的屬性時遇到了困難。我無法從資源中獲取xlink:href屬性,這是我所需要的,因爲它不會顯示在收到的數據中。似乎能夠抓住一切,但。PHP XML xlink:href屬性

這是我讀的XML:

<articles xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.spreadshirt.net" xlink:href="http://api.spreadshirt.net/api/v1/shops/800323/articles?fullData=true" offset="0" limit="50" count="16" sortField="default" sortOrder="default"> 
    <article isDuplicate="false" xlink:href="http://api.spreadshirt.net/api/v1/shops/800323/articles/100402428" id="100402428"> 
     <name>Hammer T-Shirt</name> 
     <price> 
     <vatExcluded>13.33</vatExcluded> 
     <vatIncluded>16.00</vatIncluded> 
     <vat>20.00</vat> 
     <currency xlink:href="http://api.spreadshirt.net/api/v1/currencies/2" id="2"/> 
     </price> 
     <resources> 
     <resource mediaType="png" type="preview" xlink:href="http://image.spreadshirt.net/image-server/v1/products/125642560/views/1"/> 
     </resources> 
    </article> 
</atricles> 

這是從SimpleXML的回來的數據:

SimpleXMLElement Object 
(
[@attributes] => Array 
    (
     [isDuplicate] => false 
     [id] => 27368595 
    ) 

[name] => Hammer Boxers 
[price] => SimpleXMLElement Object 
    (
     [vatExcluded] => 10.00 
     [vatIncluded] => 12.00 
     [vat] => 20.00 
     [currency] => SimpleXMLElement Object 
      (
       [@attributes] => Array 
        (
         [id] => 2 
        ) 

      ) 

    ) 

[resources] => SimpleXMLElement Object 
    (
     [resource] => SimpleXMLElement Object 
      (
       [@attributes] => Array 
        (
         [mediaType] => png 
         [type] => preview 
        ) 

      ) 

    ) 

) 

有沒有人有什麼想法?我很難過。

+0

您不顯示返回的數據。要顯示數據,使用** SimpleXMLElement **對象的' - > asXML('php:// output')'方法,不要使用'print_r'或'var_dump',這些函數隱藏任何對象的數據。 – hakre 2014-12-22 11:32:15

回答

0

isDuplicateid屬性與元素位於同一名稱空間中。

href元素位於http://www.w3.org/1999/xlink名稱空間中,如在<articles>根元素上註冊的xlink前綴所示。

要訪問命名空間的所有元素,請致電$element->attributes('http://www.w3.org/1999/xlink')

的想法是,根元素可以代替說xmlns:foobar="http://www.w3.org/1999/xlink",每個<article>將有foobar:href="..."屬性,上面的代碼仍然工作,因爲綁定的前綴僅僅是一個提高可讀性的方式。重要的是命名空間URL,而不是它的前綴。

+0

太棒了,謝謝! – 2014-12-06 21:14:49