Narendra Choudhary
Narendra Choudhary

Reputation: 134

Convert XML to object in PHP a update values of some node and again convert object to XML?

I am dealing with multiple APIs. Please find some steps.
Step1 I have got the first API's result as below. Let's say I have got the following response.

     $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 I have converted XML to 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 I have updated some node's values as follows.

    1. $cdata_as_xml->CusAccount['Action'] = 'Edit'
    2. $cdata_as_xml->CusAccount['DesiredCycleID'] = '1'
    3. $cdata_as_xml->CusAccount->AccountType->CusAccountType['AccountTypeID'] = '1000023'

Step4 Now I need this updated XML to pass in another API as body as follows:

 $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>';

But $cdata_as_xml something look like as follows:

    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 ) 
                 )
              )
            )
          )

How can I get pure XML, not XML Object?

Any help on this would be appreciated.

Thank you.

Upvotes: 0

Views: 213

Answers (1)

Honk der Hase
Honk der Hase

Reputation: 2488

Use asXML() to save XML into a file

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

If you don't specify a filename, the method will return a string, containing the well-formed XML of the Element.

Upvotes: 1

Related Questions