3

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.

Sri P
  • 375
  • 2
  • 4
  • 14

1 Answers1

3

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;
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Thank You very much Sir, but only one field is missing. i.e,, addDeatilsmissing xmlns – Sri P Jul 18 '16 at 06:41
  • Simply replace the AddDetails `createElement()` to `createElementNS()` : `$XMLDoc-> createElementNS('http://tempuri.org/', 'AddDetails');`. And namespace added to XSLT. – Parfait Jul 18 '16 at 15:57
  • Resulting empty doc – Sri P Jul 19 '16 at 12:00
  • Could you please help me – Sri P Jul 19 '16 at 12:10
  • I will wait for your valuable response,Please help me – Sri P Jul 19 '16 at 12:21
  • What do you mean -nothing echoes to screen? All because of adding the AddDetails namespace? Check if database query fetches anything. – Parfait Jul 19 '16 at 12:45
  • After adding AddDetails namespace it's resulting empty page, if i keep the code as before it's working fine. in XLST only there is a problem. Could you please modify XLST – Sri P Jul 19 '16 at 12:54
  • Apologies for the confusion but the XSLT version does not use PHP's `createElementNS()` only first DOMDOcument version uses this method. Instead, XSLT adds the namespace inside the XSLT script. – Parfait Jul 19 '16 at 18:57
  • Could you Please modify your code, Please i'm in crucial situation – Sri P Jul 20 '16 at 04:44
  • You are the only one to solve my problem, Please help me – Sri P Jul 20 '16 at 05:05
  • I got It Parfait, Thanks a ton – Sri P Jul 20 '16 at 05:34
  • TestFName TestLName test@indigo.co.in But i'm getting empty xmlns="" in Tags, how to removes those – Sri P Jul 20 '16 at 05:37
  • How to remove empty xmlns="", Please help me – Sri P Jul 20 '16 at 09:32
  • Please see updated XSLT script version. Ignore the last edit as nodes pointed to my development script. – Parfait Jul 21 '16 at 12:03
  • In Localhost xml is generating but in server empty document is generating. Please help me – Sri P Jul 22 '16 at 13:01
  • Check server's error logs. Do you have the [XSL extension enabled](http://stackoverflow.com/questions/7930726/fatal-error-class-xsltprocessor-not-found) in server's .ini file? – Parfait Jul 22 '16 at 13:16
  • in my server extension=php_xsl.dll this is not found in php.ini – Sri P Jul 22 '16 at 13:41
  • What does server error log show? If XSLT processor is not detected, this is beyond a programming issue now. Check with server admin or webhost provider. Remember `.dll` are only for Windows machines not Linux or Mac OS which use `.so` extensions (*php_xsl.so*). You might be able to just add the line to enable. Restart server with change. – Parfait Jul 22 '16 at 16:02