将XML转换为PHP中的对象,更新某个节点的值,然后再次将对象转换为XML?

问题描述 投票:0回答:1

我正在处理多个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对象?

对此有任何帮助,将不胜感激。

谢谢。

php xml
1个回答
0
投票

使用asXML()将XML保存到文件中

https://www.php.net/manual/en/simplexmlelement.asxml.php

如果不指定文件名,则该方法将返回一个字符串,其中包含元素的格式良好的XML。

© www.soinside.com 2019 - 2024. All rights reserved.