我有以下PHP代码用于将xml属性提取到php变量。
$dataPOST= '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c2b="http://cps.huawei.com/cpsinterface/c2bpayment">
<soapenv:Header/>
<soapenv:Body>
<c2b:C2BPaymentConfirmationRequest>
<TransactionType>PayBill</TransactionType>
<TransID>1234560000007031</TransID>
<TransTime>20140227082020</TransTime>
<TransAmount>123.00</TransAmount>
<BusinessShortCode>12345</BusinessShortCode>
<BillRefNumber>TX1001</BillRefNumber>
<InvoiceNumber></InvoiceNumber>
<OrgAccountBalance>12345.00</OrgAccountBalance>
<ThirdPartyTransID></ThirdPartyTransID>
<MSISDN>254722703614</MSISDN>
<KYCInfo>
<KYCName>[Personal Details][First Name]</KYCName>
<KYCValue>Hoiyor</KYCValue>
</KYCInfo>
<KYCInfo>
<KYCName>[Personal Details][Middle Name]</KYCName>
<KYCValue>G</KYCValue>
</KYCInfo>
<KYCInfo>
<KYCName>[Personal Details][Last Name]</KYCName>
<KYCValue>Chen</KYCValue>
</KYCInfo>
</c2b:C2BPaymentConfirmationRequest>
</soapenv:Body>
</soapenv:Envelope>';
$xml = @simplexml_load_string($dataPOST);
$xml->registerXPathNamespace("soap","http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace ("c2b", "http://cps.huawei.com/cpsinterface/c2bpayment");
$xml2 = $xml->xpath("//soap:Envelope/soap:Body/c2b:C2BPaymentConfirmationRequest");
$TransactionType = (string) $xml2[0]->TransactionType;
$TransID = (string) $xml2[0]->TransID;
$TransTime = (string) $xml2[0]->TransTime;
$TransAmount = (string) $xml2[0]->TransAmount;
$BusinessShortCode = (string) $xml2[0]->BusinessShortCode;
$BillRefNumber = (string) $xml2[0]->BillRefNumber;
$InvoiceNumber = (string) $xml2[0]->InvoiceNumber;
$OrgAccountBalance = (string) $xml2[0]->OrgAccountBalance;
$ThirdPartyTransID = (string) $xml2[0]->ThirdPartyTransID;
$MSISDN = (string) $xml2[0]->MSISDN;
echo 'TransactionType: '.$TransactionType.'<br/>';
echo 'TransID: '.$TransID.'<br/>';
echo 'TransTime: '.$TransTime.'<br/>';
echo 'TransAmount: '.$TransAmount.'<br/>';
echo 'BusinessShortCode: '.$BusinessShortCode.'<br/>';
echo 'BillRefNumber: '.$BillRefNumber.'<br/>';
echo 'InvoiceNumber: '.$InvoiceNumber.'<br/>';
echo 'OrgAccountBalance: '.$OrgAccountBalance.'<br/>';
echo 'ThirdPartyTransID: '.$ThirdPartyTransID.'<br/>';
echo 'MSISDN: '.$MSISDN.'<br/>';
这段代码工作正常,我可以从xml中提取大部分属性,但我现在正在努力处理最后几个属性。我该如何处理xml的以下部分并将其解析为PHP变量?
<KYCInfo>
<KYCName>[Personal Details][First Name]</KYCName>
<KYCValue>Hoiyor</KYCValue>
</KYCInfo>
<KYCInfo>
<KYCName>[Personal Details][Middle Name]</KYCName>
<KYCValue>G</KYCValue>
</KYCInfo>
<KYCInfo>
<KYCName>[Personal Details][Last Name]</KYCName>
<KYCValue>Chen</KYCValue>
</KYCInfo>
答案 0 :(得分:0)
您可以查询$xml2
变量中包含的DOM:
$query = './KYCInfo';
$elements = $xml2[0]->xpath($query);
foreach ($elements as $element) {
print $element->KYCName . PHP_EOL;
print $element->KYCValue . PHP_EOL;
}
这应该导致
[Personal Details][First Name]
Hoiyor
[Personal Details][Middle Name]
G
[Personal Details][Last Name]
Chen
您可以将其分配给变量。
答案 1 :(得分:0)
您可以使用XPath谓词来获取KYCInfo
其中子元素KYCName
值等于某个字符串,例如&#39; [个人详细信息] [姓氏]&#39; :
$query = "KYCInfo[KYCName='[Personal Details][Last Name]']/KYCValue";
$MiddleName = (string)$xml2[0]->xpath($query)[0];
<强> eval.in demo
强>
使用相同的方法获取名字和中间名......