我有一个包含如下数据的现有XML文件。 我想添加一本新书,标题,作者,年份和价格。
<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
答案 0 :(得分:0)
在这里,工作代码示例
function XMUpdate() {
try {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
}
catch (e) { try { xmlDoc = document.implementation.createDocument("", "", null); }
catch (e) { }
}
try {
xmlDoc.async = false;
xmlDoc.load("C:/test.XML");
newEle = xmlDoc.createElement("book");
newEle1 = xmlDoc.createElement("Title");
newText1 = xmlDoc.createTextNode("aaaa");
newEle1.appendChild(newText1);
newEle.appendChild(newEle1);
newEle2 = xmlDoc.createElement("author");
newText2 = xmlDoc.createTextNode("SSSSSS");
newEle2.appendChild(newText2);
newEle.appendChild(newEle2);
newEle3 = xmlDoc.createElement("year");
newText3 = xmlDoc.createTextNode("2019");
newEle3.appendChild(newText3);
newEle.appendChild(newEle3);
newEle4 = xmlDoc.createElement("price");
newText4 = xmlDoc.createTextNode("39.95");
newEle4.appendChild(newText4);
newEle.appendChild(newEle4);
xmlDoc.getElementsByTagName("bookstore")[0].appendChild(newEle);
xmlDoc.save("C:/test.XML");
}
catch (e) { alert(e.message) }
return (null);
}
答案 1 :(得分:0)
请参见下文(添加书名)。点击“演示”按钮
{description: "fr text"}