2010-12-18 77 views
0

我找這東西相當於:顯示簡單的XML PHP

$e= xmlwriter_open_uri("test.xml"); 
.... 
print htmlentities(xmlwriter_output_memory($e)); 

現在這個打印允許顯示最新的XML列表插入表中。

但我用我的簡單的XML(結合$ DOM的格式)我不知道如何顯示此。儘管這會產生我希望進入xml的正確輸出,但我如何顯示下面的xml?類似於打印或?

目的是將xml的值顯示到表中。

$dom = new DOMDocument('1.0'); 
$dom->preserveWhiteSpace = false; 
$dom->formatOutput = true; 

$xml = new SimpleXMLElement('<test></test>');  
    $one= $xml->addChild('enemy', 'yes');   
    $two= $xml->addChild('friend', 'maybe');   

$dom->loadXML($xml->asXML()); 
$dom->save('test.xml'); 

問候

回答

1

你並不需要字符串化(技術術語!)的SimpleXMLElement將其加載到一個DOMDocument,其實這是一個可怕的想法(不過,我原諒你)。

$xml = new SimpleXMLElement('<test></test>'); 
$one= $xml->addChild('enemy', 'yes'); 
$two= $xml->addChild('friend', 'maybe'); 

// Get the DOMDocument associated with this XML 
$dom = dom_import_simplexml($xml)->ownerDocument; 
$dom->preserveWhiteSpace = false; 
$dom->formatOutput = true; 

echo $dom->saveXML(); // or echo htmlentities($dom->saveXML()) if you really must 

有關從SimpleXMLElement檢索DOMElement(及其DOMDocument)更多信息可以在文檔中找到dom_import_simplexml()