Search code examples
phpxmlsimplexml

Convert simpleXML object to string - php


I want to convert my XML object to string format. Currently I have an XML object. I want to concatenate it to a string. To be able to do that, I want to convert the XML object to a string.

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
 <soapenv:Header />
 <soapenv:Body>
  <ser:processTrans>
     <xmlValue>
         <![CDATA[
            <ebpacket> 
                <head> 
                    <packettype> UserAuthorization</packettype>
                    <staffcode> ePay_UserName  </staffcode> 
                    <pwd>  ePay_Password </pwd> 
                    <env>  ePay_Evnironment  </env> 
                </head> 
                <body>
                    <email> _username  </email>
                    <loginpwd>  _password  </loginpwd>
                    <deviceid>  DeviceId  </deviceid>
                    <tokenid> TokenId  </tokenid>
                    <cip>  CustomerIpAddress  </cip>
                </body>
            </ebpacket>
        ]]>
    </xmlValue>
  </ser:processTrans>

This is the simpleXML object. I want to convert it to a string to perform string functions on this.


Solution

  • You could use $str = $xmlObject->asXML(); to get the XML string.

    If you want to get the nodes contents, you also could use strip_tags($str);.