how to generate XML with soap envelope and body tag using php

Sri P

I'm unable to generate XMl with Soap Envelop and Body Tag, here is the code,

$rootElement  = $XMLDoc->createElement('AddDetails');
        $rootNode = $XMLDoc->appendChild($rootElement);

        while($result_array =  $result->fetch_assoc())  {
            $StockCount++;
              foreach($result_array as $key => $value)  {
                  $value=trim($value);
                  if($value=="NULL" || $value=="" ||$value==-1){
                        $value="";
                  }
                    $value=htmlentities($value);//For validating & chars
                    $rootNode->appendChild($XMLDoc->createElement($key,$value));
              }
           }

The above code resulting XMl with Key and values like

<AddDetails>
<First_Name>TestFName</First_Name>
<Last_Name>TestLName</Last_Name>
</AddDetails>

but i want to generate XML like

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddDetails xmlns="http://tempuri.org/">
<First_Name>TestFName</First_Name>
<Last_Name>TestLName</Last_Name>
</AddDetails>
</soap:Body>
</soap:Envelope>

Please help me.

Parfait

SOAP headers are essentially XML namespaces which can be added using DOMDocument's createElementNS. So simply add them before the XML content:

$XMLDoc = new DOMDocument('1.0', 'UTF-8');
$XMLDoc->preserveWhiteSpace = false;
$XMLDoc->formatOutput = true;

// SOAP ENVELOPE ELEMENT AND ATTRIBUTES
$soap = $XMLDoc->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'soap:Envelope');
$XMLDoc->appendChild($soap);

$soap->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$soap->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
$soap->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:soap', 'http://schemas.xmlsoap.org/soap/envelope/');

// SOAP BODY
$body = $XMLDoc->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'soap:Body');

// XML CONTENT
$rootElement = $XMLDoc->createElementNS('http://tempuri.org/', 'AddDetails');
$rootNode = $body->appendChild($rootElement);

while($result_array =  $result->fetch_assoc())  {
    $StockCount++;
      foreach($result_array as $key => $value)  {
          $value=trim($value);
          if($value=="NULL" || $value=="" ||$value==-1){
                $value="";
          }
        $value=htmlentities($value);//For validating & chars
        $rootNode->appendChild($XMLDoc->createElement($key,$value));
    }
}

Alternatively, consider XSLT, the special purpose language to transform XML files. This approach uses your current setup as is and transforms it with XSLT wrapping SOAP headers:

// XML CONTENT
$rootElement  = $XMLDoc->createElement('AddDetails');
$rootNode = $XMLDoc->appendChild($rootElement);

while($result_array =  $result->fetch_assoc())  {
    $StockCount++;
      foreach($result_array as $key => $value)  {
          $value=trim($value);
          if($value=="NULL" || $value=="" ||$value==-1){
                $value="";
          }
        $value=htmlentities($value);//For validating & chars
        $rootNode->appendChild($XMLDoc->createElement($key,$value));
    }
}

// XSL SCRIPT
$XSLDoc = new DOMDocument('1.0', 'UTF-8');

$xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
            <xsl:output version="1.0" encoding="UTF-8" indent="yes" />
            <xsl:strip-space elements="*" />

              <xsl:template match="/">    
                  <xsl:apply-templates select="AddDetails"/>    
              </xsl:template>

              <xsl:template match="AddDetails">
                <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                  <soap:Body>
                    <AddDetails xmlns="http://tempuri.org/">
                        <xsl:apply-templates select="@*|node()"/>
                    </AddDetails>
                  </soap:Body>
                </soap:Envelope>
              </xsl:template>

              <xsl:template match="*">
                  <xsl:element name="{local-name()}" namespace="http://tempuri.org/">
                        <xsl:apply-templates select="@*|node()"/>
                  </xsl:element>
              </xsl:template>   

            </xsl:transform>';

$XSLDoc->loadXML($xslstr);

// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($XSLDoc);

// Transform XML source
$newXML = $proc->transformToXML($XMLDoc);

echo $newXML;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Removing xmlns, SOAP:Envelope, SOAP:Body, from xml using XSLT

From Dev

Constructing a SOAP envelope using LINQ to XML

From Dev

Constructing a SOAP envelope using LINQ to XML

From Dev

xml parsing of a soap envelope using XmlDocument

From Dev

Java Generate SOAP Envelope

From Dev

PHP & XML - How to generate a soap request in PHP from this XML?

From Dev

php soap envelope namespace <soap:Envelope and <SOAP-ENV:Envelope

From Dev

php soap envelope namespace <soap:Envelope and <SOAP-ENV:Envelope

From Dev

How do I remove a soap envelope using JDOM and return the remainder of the XML as a String?

From Dev

How to create proper soap envelope (request xml) in code while using KSoap2?

From Dev

Wrap XML in SOAP envelope in .net

From Dev

Soap UI | How to parse nodes of XML (response) in Soap UI which has no soap envelope

From Dev

PHP Soap Request: XML Body missing in the request

From Dev

Powershell invoke-webrequest with xml soap envelope body containing £ char errors

From Dev

How to read soapenv:Envelope xml node in php?

From Dev

Simple framework skip soap envelope and body

From Dev

How to add attributes to SOAP envelope part, using suds?

From Dev

how to void an envelope using C# / XML?

From Dev

How to check if XML tag is empty using PHP?

From Dev

Send 'XML data as string' in Soap XML envelope

From Dev

Send 'XML data as string' in Soap XML envelope

From Dev

How to generate end tag for empty element in XML using JAXB

From Dev

How to generate xml tag as full class name using jaxb?

From Dev

Generate XML using PHP

From Dev

How to parse Soap Envelope in Golang ?

From Dev

How to get all the <a> tags from the <body> tag using PHP DOM?

From Dev

How to generate the following SOAP XML request?

From Dev

How to generate the following SOAP XML request?

From Dev

How to send XML soap request using php curl

Related Related

  1. 1

    Removing xmlns, SOAP:Envelope, SOAP:Body, from xml using XSLT

  2. 2

    Constructing a SOAP envelope using LINQ to XML

  3. 3

    Constructing a SOAP envelope using LINQ to XML

  4. 4

    xml parsing of a soap envelope using XmlDocument

  5. 5

    Java Generate SOAP Envelope

  6. 6

    PHP & XML - How to generate a soap request in PHP from this XML?

  7. 7

    php soap envelope namespace <soap:Envelope and <SOAP-ENV:Envelope

  8. 8

    php soap envelope namespace <soap:Envelope and <SOAP-ENV:Envelope

  9. 9

    How do I remove a soap envelope using JDOM and return the remainder of the XML as a String?

  10. 10

    How to create proper soap envelope (request xml) in code while using KSoap2?

  11. 11

    Wrap XML in SOAP envelope in .net

  12. 12

    Soap UI | How to parse nodes of XML (response) in Soap UI which has no soap envelope

  13. 13

    PHP Soap Request: XML Body missing in the request

  14. 14

    Powershell invoke-webrequest with xml soap envelope body containing £ char errors

  15. 15

    How to read soapenv:Envelope xml node in php?

  16. 16

    Simple framework skip soap envelope and body

  17. 17

    How to add attributes to SOAP envelope part, using suds?

  18. 18

    how to void an envelope using C# / XML?

  19. 19

    How to check if XML tag is empty using PHP?

  20. 20

    Send 'XML data as string' in Soap XML envelope

  21. 21

    Send 'XML data as string' in Soap XML envelope

  22. 22

    How to generate end tag for empty element in XML using JAXB

  23. 23

    How to generate xml tag as full class name using jaxb?

  24. 24

    Generate XML using PHP

  25. 25

    How to parse Soap Envelope in Golang ?

  26. 26

    How to get all the <a> tags from the <body> tag using PHP DOM?

  27. 27

    How to generate the following SOAP XML request?

  28. 28

    How to generate the following SOAP XML request?

  29. 29

    How to send XML soap request using php curl

HotTag

Archive