如何在 PHP 中使用 simplexml_load_string 后检索 XML 的第一个节点?
How to retrieve the first node of a XML after use simplexml_load_string in PHP?
我有以下 xml,其中第一个节点是 <Cancellation>
<?xml version='1.0' encoding='UTF-8'?>
<Cancellation>
<version>message-version</version>
<customerID>customer-identifier</customerID>
<invoiceID>invoice-number</invoiceID>
<cancellationDate>yyyy-mm-dd</cancellationDate>
<reason>reason</reason>
<reasonCode>reason-code</reasonCode>
<attempts>attempts-count</attempts>
<merchantID>rocketgate merchant-identifier</merchantID>
<merchantSiteID>site-id</merchantSiteID>
<udf01>user-data</udf01>
</Cancellation>
最终我可以有一个类似的 xml 但是对于像注册这样的完全不同的过程,如下所示:
<?xml version='1.0' encoding='UTF-8'?>
<Registration>
<version>message-version</version>
<customerID>customer-identifier</customerID>
<invoiceID>invoice-number</invoiceID>
<merchantID>rocketgate merchant-identifier</merchantID>
<merchantSiteID>site-id</merchantSiteID>
<udf01>user-data</udf01>
</Registration>
我需要在 if 条件下捕获第一个节点以重定向到适当的路径。
在这个 link 之后,我从发送的 xml:
中得到了一个数组
$xmlString = trim(file_get_contents('php://input'));
$xmlObj = simplexml_load_string($xmlString);
$xmlJSON = json_encode($xmlObj);
$xmlArray = json_decode($xmlJSON, true);
但问题是在将 xml 字符串传递给对象后,我看不到第一个 <Cancellation>
或 <Registration>
节点。
甚至 php doc 中的示例也没有显示第一个节点。
我需要测试它是取消还是注册。我怎么能那样做?
要在 XML 中查找根元素的标签名称,只需在简单 XML 元素上使用 getName()
...
echo $xmlObj->getName();
我有以下 xml,其中第一个节点是 <Cancellation>
<?xml version='1.0' encoding='UTF-8'?>
<Cancellation>
<version>message-version</version>
<customerID>customer-identifier</customerID>
<invoiceID>invoice-number</invoiceID>
<cancellationDate>yyyy-mm-dd</cancellationDate>
<reason>reason</reason>
<reasonCode>reason-code</reasonCode>
<attempts>attempts-count</attempts>
<merchantID>rocketgate merchant-identifier</merchantID>
<merchantSiteID>site-id</merchantSiteID>
<udf01>user-data</udf01>
</Cancellation>
最终我可以有一个类似的 xml 但是对于像注册这样的完全不同的过程,如下所示:
<?xml version='1.0' encoding='UTF-8'?>
<Registration>
<version>message-version</version>
<customerID>customer-identifier</customerID>
<invoiceID>invoice-number</invoiceID>
<merchantID>rocketgate merchant-identifier</merchantID>
<merchantSiteID>site-id</merchantSiteID>
<udf01>user-data</udf01>
</Registration>
我需要在 if 条件下捕获第一个节点以重定向到适当的路径。
在这个 link 之后,我从发送的 xml:
中得到了一个数组$xmlString = trim(file_get_contents('php://input'));
$xmlObj = simplexml_load_string($xmlString);
$xmlJSON = json_encode($xmlObj);
$xmlArray = json_decode($xmlJSON, true);
但问题是在将 xml 字符串传递给对象后,我看不到第一个 <Cancellation>
或 <Registration>
节点。
甚至 php doc 中的示例也没有显示第一个节点。
我需要测试它是取消还是注册。我怎么能那样做?
要在 XML 中查找根元素的标签名称,只需在简单 XML 元素上使用 getName()
...
echo $xmlObj->getName();