0
我試圖將新的子節點添加到xml文件;到目前爲止,我有這樣的代碼:將使用libxmljs(Node.js)的子節點添加到XML文件
var libxml = require('libxmljs');
var xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<root>' +
'<child foo="bar">' +
'<grandchild baz="fizbuzz"><blah>grandchild content</blah></grandchild>' +
'</child>' +
'<child foo="bar1">' +
'<grandchild baz="fizbuzz">grandchild content 1</grandchild>' +
'</child>' +
'<child foo="bar3">' +
'<grandchild baz="fizbuzz3">grandchild content 3</grandchild>' +
'</child>' +
'<sibling>with content!</sibling>' +
'</root>';
var xmlDoc = libxml.parseXml(xml);
var allxml = xmlDoc.root(); //store all nodes as allxml
var allNodes = xmlDoc.childNodes(); //all child nodes to array
var elem = xmlDoc.node('name1');
var newChild = libxml.Element(xmlDoc, 'new-child');
elem.addChild(newChild);
,但我得到以下錯誤,當我運行此:
return this._root(elem);
^
Error: Holder document already has a root node
有誰知道什麼是怎麼回事呢?
感謝編輯@Knownasliya –