我有 this kind of xml文档。
我的问题是我无法通过 simplexml 读取标签,因为 namespace 不同。示例 BuyerCustomerParty->Party->Person->FamilyName
标签。 BuyerCustomerParty
, Party
和 Person
在下 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";
如何读取标签?
最佳答案
我的解决方案是将 cbc child 包括在内。
echo $fg = $cac->BuyerCustomerParty->Party->Person->children('cbc',TRUE)->FamilyName;
关于使用 UBL/不同命名空间读取 PHP simplexml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21765994/