我正在处理多个API。请找到一些步骤。Step1我得到了第一个API的结果,如下所示。假设我收到了以下回复。
$xml = '<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<Response
xmlns="http://schemas.martin-group.com/test">
<Result>
<![CDATA[
<AccountInfo><CusAccount Action="Add" AccountID="2046369" DesiredCycleID="0" Name=""
Corporation="" Cycle=""><AccountType><CusAccountType AccountTypeID="100001" Version="1"
AccountTypeCode="BUS " AccountType="Business" MonthlyBillingMethod="P" /></AccountType>
<InvoiceFormat><ICInvoiceFormat InvoiceFormatID="1" UserID="10490655"
AddressAdjustmentY="0" AddressWidth="3500"/></InvoiceFormat></CusAccount>
</AccountInfo>]]>
</Result>
</Response>
</soap:Body>
</soap:Envelope>';
Step2我已经将XML转换为XML Object。
$soap = simplexml_load_string($xml);
$soap->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$object = $soap->xpath('//soap:Envelope/soap:Body')[0]->Response;
$cdata = $object->Result;
$cdata_as_xml = simplexml_load_string($cdata);
Step3我已经如下更新了一些节点的值。
1. $cdata_as_xml->CusAccount['Action'] = 'Edit'
2. $cdata_as_xml->CusAccount['DesiredCycleID'] = '1'
3. $cdata_as_xml->CusAccount->AccountType->CusAccountType['AccountTypeID'] = '1000023'
Step4现在,我需要此更新的XML来将另一个API作为主体传递,如下所示:
$xml = '<CusAccount Action="Edit" AccountID="2046369" DesiredCycleID="1" Name=""
Corporation="" Cycle=""><AccountType><CusAccountType AccountTypeID="1000023" Version="1"
AccountTypeCode="BUS " AccountType="Business" MonthlyBillingMethod="P" /></AccountType>
<InvoiceFormat><ICInvoiceFormat InvoiceFormatID="1" UserID="10490655"
AddressAdjustmentY="0" AddressWidth="3500"/></InvoiceFormat></CusAccount>';
但是$ cdata_as_xml如下所示:
SimpleXMLElement Object (
[CusAccount] => SimpleXMLElement Object
( [@attributes] => Array ( [Action] => Edit [AccountID] => 2046414 [DesiredCycleID] => 1
[Name] => [Corporation] => [Cycle] => )
[AccountType] => SimpleXMLElement Object
( [CusAccountType] => SimpleXMLElement Object (
[@attributes] => Array ( [AccountTypeID] => 1000023 [Version] => 1
[ModifyDate] => 5/21/2009 9:46:00 AM [UserID] => 10120452
[AccountTypeCode] => BUS [AccountType] => Business
[MonthlyBillingMethod] => P
)
)
)
[InvoiceFormat] => SimpleXMLElement Object
( [ICInvoiceFormat] => SimpleXMLElement Object
( [@attributes] => Array ( [InvoiceFormatID] => 1 [UserID] => 10490655
[AddressAdjustmentY] => 0 [AddressWidth] => 3500 )
)
)
)
)
如何获得纯XML,而不是XML对象?
对此有任何帮助,将不胜感激。
谢谢。
使用asXML()
将XML保存到文件中
https://www.php.net/manual/en/simplexmlelement.asxml.php
如果不指定文件名,则该方法将返回一个字符串,其中包含元素的格式良好的XML。