如何使用php生成带有soap信封和body标签的XML

时间:2022-11-23 08:26:15

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

我无法使用Soap Envelop和Body Tag生成XMl,这是代码,

$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

上面的代码产生了带有Key和值的XMl

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

but i want to generate XML like

但我想生成XML之类的

<?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.

请帮帮我。

1 个解决方案

#1


1  

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

SOAP标头本质上是XML命名空间,可以使用DOMDocument的createElementNS添加。所以只需在XML内容之前添加它们:

$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:

或者,考虑XSLT,它是转换XML文件的特殊用途语言。此方法按原样使用您当前的设置,并使用XSLT包装SOAP标头对其进行转换:

// 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;

#1


1  

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

SOAP标头本质上是XML命名空间,可以使用DOMDocument的createElementNS添加。所以只需在XML内容之前添加它们:

$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:

或者,考虑XSLT,它是转换XML文件的特殊用途语言。此方法按原样使用您当前的设置,并使用XSLT包装SOAP标头对其进行转换:

// 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;