2016-05-17 5 views
0

XMLファイルをインデントしようとしていますが、このエラーが原因でできません。 この問題はなぜ発生しますか?PHP DOMDocument:致命的なエラー:定義されていないメソッドを呼び出すDOMElement :: save()

The problem

これは私のコードです:

<?php 
$xmlstr = 'xmlfile.xml'; 

$sxe = new SimpleXMLElement($xmlstr, null, true); 

$lastID = (int)$sxe->xpath("//tip[last()]/tipID")[0] + 1; 

$tip = $sxe->addChild('tip'); 
$tip->addChild('tipID', $lastID); 
$tip->addChild('tiptitle', 'Title:'); 
$sxe->asXML($xmlstr); 

$xmlDom = dom_import_simplexml($sxe); 
$xmlDom->formatOutput = true; 
$xmlDom->save($xmlstr); 

?> 

私は多くの研究を行ってきたと私は答えを見つけることができませんでした。

+0

は、残念ながらそれを解決しませんでした。エラーは同じですが、saveではなくsaveXMLで保存されます。 –

答えて

0

dom_import_simplexml function返さないsave方法を持っていないDOMElementのインスタンス、:前のDOMDocumentを行います。代わりにあなたが必要とするものはDOMDocumentです。になりますsaveメソッドを持っています。

DOMElementDOMNodeのタイプであり、ownerDocument propertyのタイプであるため、幸いなことに、1つから他のものに到達するのは簡単です。 formatOutput属性もDOMDocumentの一部であることに注意してくださいので、あなたが必要とするものである。この:splash58 @

$xmlDom = dom_import_simplexml($sxe)->ownerDocument; 
$xmlDom->formatOutput = true; 
$xmlDom->save($xmlstr); 
1

DOMElementにはxmlを保存するメソッドがありませんが、DOMDocumentはそれを行います。

$xmlDom = dom_import_simplexml($sxe); 

$dom = new DOMDocument(); 
$dom_sxe = $dom->importNode($xmlDom, true); 
$dom_sxe = $dom->appendChild($xmlDom); 
$Dom->formatOutput = true; 
echo $dom->saveXML(); 
関連する問題