2016-07-14 1 views
2

を使用してSOAPエンベロープとbodyタグでXMLを生成する方法を、ここでは、コードですは、私は石鹸エンベロープとボディタグでXMLを生成することができないんだPHP

$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)); 
       } 
      } 

キーと値を持つXMLを結果として上記のコードを以下のような

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

が、私は

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

のようなXMLを生成するには、私を助けてください。

+0

ソースデータはどこですか?それには何が含まれていますか? – Parfait

+0

データベースからのデータをソートしました。($ result_array = $ result-> fetch_assoc()) –

答えて

1

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)); 
    } 
} 
はまた、XMLファイルを変換するために XSLT、特別な目的の言語を考えます。この方法では、現在の設定をそのまま使用して、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; 
+0

ありがとうございます。つまり、 addDeatilsmissing xmlns –

+0

AddDetailsの 'createElement()'を 'createElementNS()'に置き換えるだけです: '$ XMLDoc-> createElementNS( 'http:// tempuri .org/'、' AddDetails ')); XSLTに名前空間が追加されました。 – Parfait

+0

結果の空のドキュメント –

関連する問題