2016-03-05 33 views
2

下面这篇文章Saving form data to an existing XML-file using PHP我想能够编辑XML头标记

我想知道我怎么可以保存多个变量到XML文件中。使用描述的方法我只能保存最后一个。

我发现了一个方法的工作,因为我想

$xml="\n\t\t<Bares>\n\t\t";  
    { 
     while ($stmt->fetch()){ 

       $xml .="<Bar>\n\t\t"; 
       $xml .= "<nome>".$nome."</nome>\n\t\t"; 
       $xml .= "<morada>".$morada."</morada>\n\t\t"; 
       $xml .= "<nif>".$nif."</nif>\n\t\t"; 
       $xml .= "<telefone>".$telefone."</telefone>\n\t\t"; 
       $xml .= "<email>".$email."</email>\n\t\t"; 
       $xml .= "<imgid>".$imgid."</imgid>\n\t\t"; 
       $xml.="</Bar>\n\t"; 

     } 

      $xml.="</Bares>\n\r"; 

      $doc = new DOMDocument('1.0'); 
      $doc->formatOutput = true; 
      $doc->preserveWhiteSpace = true; 
      $doc->loadXML($xml, LIBXML_NOBLANKS); 
      $doc->save('dados.xml');     

,但我想能够编辑XML头标记(XML版本=“1.0”?),我不能做这样

+0

你为什么不使用DOM文档的[的createElement()](http://php.net/manual/en/domdocument.createelement.php )和[appendChild()](http://php.net/manual/en/domnode.appendchild.php)方法? – Parfait

+0

因为这样我无法保存xml文档中的所有元素,只保存了最后一个元素。这是我的: '$ xml-> bar =“”; \t \t \t \t $ xml-> bar-> addChild('nome_bar',$ nome); \t \t \t \t $ xml-> bar-> addChild('morada_bar',$ morada); \t \t \t \t $ xml-> bar-> addChild('nif_bar',$ nif); \t \t \t \t $ xml-> bar-> addChild('telefone_bar',$ telefone); \t \t \t \t $ xml-> bar-> addChild('email_bar',$ email); \t \t \t \t $ xml-> bar-> addChild('imageId',$ imgid); $ doc = new DOMDocument('1.0'); \t \t \t \t $ doc-> formatOutput = true; \t \t \t \t $ doc-> preserveWhiteSpace = true; \t \t \t \t $ doc-> loadXML($ xml-> asXML(),LIBXML_NOBLANKS); \t \t \t \t $ doc->保存( 'dados.xml');' – hirokumata

+0

那是因为你不重复加''作为子根''。 – Parfait

回答

0

再次考虑使用DOMDocument()方法,而不是使用字符串连接构建XML。使用PHP的信息手册,显示了如何添加一个Document Type Declaration (DTD)XSLT processing line

// Create an instance of the DOMImplementation class 
$imp = new DOMImplementation;  
// Create a DOMDocumentType instance 
$dtd = $imp->createDocumentType('graph', '', 'graph.dtd'); 

// Create a DOMDocument instance 
$doc = $imp->createDocument();  
// Set other properties 
$doc->encoding = 'UTF-8'; $doc->standalone = true; 
$doc->formatOutput = true; $doc->preserveWhiteSpace = true; 

// Create an XSLT adding processing line 
$xslt = $doc->createProcessingInstruction('xml-stylesheet', 
              'type="text/xsl" href="base.xsl"');  
// Append XSLT instruction to the doc 
$doc->appendChild($xslt); 
// Append DTD to the doc 
$doc->appendChild($dtd); 

// Create root 
$root = $doc->appendChild($doc->createElement('Bares')); 

// Iteratively append the elements (with values) 
while ($stmt->fetch()){       
    $barNode = $root->appendChild($doc->createElement('bare')); 
    $nomeNode = $barNode->appendChild($doc->createElement('nom', $nome)); 
    $moradaNode = $barNode->appendChild($doc->createElement('morada', $morada));     
    $nifNode = $barNode->appendChild($doc->createElement('nif', $nif)); 
    $telefoneNode = $barNode->appendChild($doc->createElement('telefone', $telefone)); 
    $emailNode = $barNode->appendChild($doc->createElement('email', $email)); 
    $imgidNode = $barNode->appendChild($doc->createElement('imgid', $imgid));   
} 

$doc->save('dados.xml'); 
+0

伟大的解决方案完美地工作。谢谢!! – hirokumata

+0

太棒了!请接受以确认解决方案。 – Parfait

相关问题