2015-09-25 125 views
0

我想解析一個xml文件,我可以得到d3pp1:key和d3p1:值好的。simplexml卡住解析xml

foreach ($xml_contact->Attributes->KeyValuePairOfstringanyType as $node) { 
    $key = (string)$node->children('d3p1', TRUE)->key; 
    $value = (string)$node->children('d3p1', TRUE)->value; 

    do_stuff($key, $value); 
} 

,但我也需要得到這個9ccaa69b-fced-e411-80da-00155d0a0806 ,我努力弄清楚如何引用它。

我已經嘗試了各種各樣的化身沿着這些線路

$node->children('d3p1', TRUE)->value->Id 

我在做什麼錯?

<KeyValuePairOfstringanyType> 
    <d3p1:key>birthdate</d3p1:key> 
    <d3p1:value xmlns:d5p1="http://www.w3.org/2001/XMLSchema" i:type="d5p1:dateTime">1940-12-10T11:00:00Z</d3p1:value> 
</KeyValuePairOfstringanyType> 
<KeyValuePairOfstringanyType> 
    <d3p1:key>parentcustomerid</d3p1:key> 
    <d3p1:value i:type="EntityReference"> 
    <Id>9ccaa69b-fced-e411-80da-00155d0a0806</Id> 
    <KeyAttributes xmlns:d6p1="http://schemas.microsoft.com/xrm/7.1/Contracts"/> 
    <LogicalName>account</LogicalName> 
    <Name>Test ABC</Name> 
    <RowVersion i:nil="true"/> 
    </d3p1:value> 
</KeyValuePairOfstringanyType> 
+0

你可能需要做的XML對象的registerXPathNamespace? – RossW

+0

您顯示的XML片段有兩個根元素。這只是它的一部分或整個事情?如果是整件事情,那是無效的。 – Gordon

回答

1

Id元素沒有命名空間前綴,所以在文檔的默認命名空間,或在沒有命名空間,如果該文件沒有默認命名空間。您需要再次調用->children()才能切換到正確的名稱空間,因爲SimpleXML當前正在名稱空間中尋找更多的前綴爲d3p1的節點。

如果沒有默認命名空間,你只需要通過NULL

$node->children('d3p1', TRUE)->value->children(NULL)->Id 
+0

謝謝,我確實嘗試了value-> children(),但沒有null參數,現在工作很好 – bumperbox