2014-02-13 14 views
0

我有this kind of xml文件。Php simplexml讀取UBL /不同命名空間

我的問題是我不能用simplexml讀取標籤,因爲不同的名稱空間。示例BuyerCustomerParty->Party->Person->FamilyName標記。 BuyerCustomerPartyPartyPerson位於cac-名稱空間下,但FamilyName位於cbc-名稱空間下。奇怪的是,我可以寫入標籤並替換內容,但在此之前無法閱讀。

這裏是一些代碼也:

$sxe = simplexml_load_string($value); 
$namespaces = $sxe->getDocNamespaces(); 
$sxe->registerXPathNamespace('cbc', $namespaces['cbc']); 
$cbc = $sxe->children($namespaces['cbc']); 

//THIS PRINTS THE RIGHT VALUE    
$cbc->IssueDate; 

$sxe->registerXPathNamespace('cac', $namespaces['cac']); 
$cac = $sxe->children($namespaces['cac']); 

//BUT THIS PRINTS NOTHING 
echo $fg = $cac->BuyerCustomerParty->Party->Person->FamilyName; 

//BUT IF I CHANGE THE VALUE OF THE TAG... I CAN ACCESS THE TAG 
$cac->BuyerCustomerParty->Party->Person->FamilyName = "value"; 

我怎樣才能讀取標籤?

+0

'xpath'可能有幫助。看到我的答案在這裏:http://stackoverflow.com/questions/21788253/simplexml-is-not-parsing-my-epp-xml-messages – michi

+0

可能重複的[PHP命名空間simplexml問題](http://stackoverflow.com /問題/ 2098170/PHP的名稱空間的SimpleXML-問題) – IMSoP

回答

0

我的解決方案是將cbc兒童包括在路徑中。

echo $fg = $cac->BuyerCustomerParty->Party->Person->children('cbc',TRUE)->FamilyName;