[英]How do I parse multiple xml attributes to php variables?
I have the following PHP code that I am using to extract xml attributeds to a php variables. 我有以下PHP代码,可用于提取归因于php变量的xml。
$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/>';
This code is working fine and I am able to extract most of the attributes from the xml but I am now struggling with the last few attributes. 这段代码运行良好,我能够从xml中提取大多数属性,但是我现在正为最后几个属性而苦苦挣扎。 How should I handle the following part of the xml and parse it to PHP variable?
我应该如何处理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>
You can query the DOM held in your $xml2
variable: 您可以查询
$xml2
变量中包含的DOM:
$query = './KYCInfo';
$elements = $xml2[0]->xpath($query);
foreach ($elements as $element) {
print $element->KYCName . PHP_EOL;
print $element->KYCValue . PHP_EOL;
}
This should result in 这应该导致
[Personal Details][First Name]
Hoiyor
[Personal Details][Middle Name]
G
[Personal Details][Last Name]
Chen
which you can assign to variables. 您可以将其分配给变量。
You can use XPath predicate to get KYCInfo
where child element KYCName
value equals certain string, for example, '[Personal Details][Last Name]' : 您可以使用XPath谓词获取
KYCInfo
,其中子元素KYCName
值等于某些字符串,例如'[Personal Details] [Last Name]':
$query = "KYCInfo[KYCName='[Personal Details][Last Name]']/KYCValue";
$MiddleName = (string)$xml2[0]->xpath($query)[0];
Use the same approach to get first name and middle name... 使用相同的方法获取名字和中间名...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.