假設我有以下 .xml 檔案:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name>Foo</name>
</item>
<item>
<name>Bar</name>
</item>
</root>
在這個示例檔案中,我試圖在最后一個 node 之后將新節點附加<item>到<root>node <item>。
我試圖在 .xml 檔案中節點中的<item>最后一個<item>節點之后追加新創建的節點<root>。
<?php
$file = new DOMDocument;
$file->load("xml.xml");
$file->loadXML($file->saveXML());
$root = $file->getElementsByTagName('root')->item(0);
foreach (["Foo_1", "Bar_2", "Foo_3", "Bar_4"] as $val) {
$item = new DOMElement('item');
$item->appendChild(new DOMElement('name', $val));
$root->appendChild(item);
}
?>
但我收到一個錯誤:
致命錯誤:未捕獲的錯誤:在 C:\Users\pfort\Desktop\p.php:12 中呼叫成員函式 appendChild() 為 null 堆疊跟蹤:#0 {main} 拋出在 C:\Users\user_acer\Desktop\第 12 行的 p.php
我究竟做錯了什么?
uj5u.com熱心網友回復:
您的示例代碼存在多個問題。我將首先解決您收到的錯誤:
<terminy>您的示例 XML 中沒有元素,因此
$root = $file->getElementsByTagName('terminy')->item(0);
會回來null。這就是為什么你會收到
在 null 上呼叫成員函式 appendChild()
錯誤在
$root->appendChild(item);
此外,item是一個錯字,因為它不是一個有效的變數名(而是一個不存在的常量的名稱);你的意思是$item。
我假設“terminy”的意思類似于您母語中的“root”,并且您實際上打算撰寫
$root = $file->getElementsByTagName('root')->item(0);
順便說一句:如果你想參考一個 XML 檔案的根節點,你也可以使用$file->docomentElement.
但是,您的示例代碼還存在其他問題:
$file->load("xml.xml");
$file->loadXML($file->saveXML()); // why are you reloading it in this way?
最后一行是不必要的。您正在重新加載相同的 XML。是為了格式化嗎?如果是這樣,還有更好的選擇:
$file->preserveWhiteSpace = false;
$file->formatOutput = true;
$file->load("xml.xml");
最后:您不能將子節點附加到尚未與檔案關聯的節點。因此,要創建新專案并將其與檔案關聯,您可以執行以下操作(推薦):
// automatically associate new nodes with document
$item = $file->createElement('item');
$item->appendChild($file->createElement('name', $val));
或(更麻煩):
// import nodes to associate them with document
$item = $file->importNode(new DOMElement('item'));
$item->appendChild($file->importNode(new DOMElement('name', $val)));
所以,把它們放在一起就變成了:
<?php
$xml = <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name>Foo</name>
</item>
<item>
<name>Bar</name>
</item>
</root>
XML;
$file = new DOMDocument;
$file->preserveWhiteSpace = false;
$file->formatOutput = true;
$file->loadXML($xml); // (for demo purpose loading above XML) replace this with $file->load("xml.xml"); in your actual code
$root = $file->documentElement;
foreach (["Foo_1", "Bar_2", "Foo_3", "Bar_4"] as $val) {
$item = $file->createElement('item');
$item->appendChild($file->createElement('name', $val));
$root->appendChild($item);
}
echo $file->saveXML();
uj5u.com熱心網友回復:
**問題解決了**
我在這個問題上浪費了太多時間。好訊息是,我已經知道如何獲得我需要的東西。在這里,我提供了一個解決方案——適用于需要解決相同問題的每個人。也許這個解決方案對任何需要它的人都有用。
<?php
// snippet of xml temple
$xml = <<<XML
<item date="%s" status="%s">
<name>%s</name>
</item>
XML;
// prepare snippet
$xmlSnippet = sprintf($xml, "2022-11-21", 0, "Foo Bar");
// new DOMDocument
$dom = new DOMDocument;
$dom->preserveWhiteSpace = 0;
$dom->formatOutput = 1;
// load of .xml file content and load to DOMDocument object
$file = simplexml_load_file("xml.xml");
$dom->loadXML($file->asXML());
// creating of fragment from snippet
$fragment = $dom->createDocumentFragment();
$fragment->appendXML($xmlSnippet);
//append the snippet to the DOMDocument
// and save it to the xml.xml file
$dom->documentElement->appendChild($fragment);
$dom->save("xml.xml");
?>
結果:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/363074.html
上一篇:使用python更新XML檔案

