我正在尝试将新的子节点添加到 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
有谁知道这里发生了什么?