gpt4 book ai didi

java - 使用 Java DOM 将新元素添加到 xml 文件

转载 作者:行者123 更新时间:2023-12-02 13:22:59 24 4
gpt4 key购买 nike

我一直在使用一个简单的 cd 目录 XML 文件:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>

用这个方法我想创建一个新的CD元素:

private static void addCd(File xml) throws ParserConfigurationException, TransformerConfigurationException, TransformerException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(xml);

Element cd = document.createElement("CD");

document.appendChild(cd);
for (int i = 1; i <= 3; i++) {
Element title = document.createElement("TITLE");
Element artist = document.createElement("ARTIST");
Element country = document.createElement("COUNTRY");
Element company = document.createElement("COMPANY");
Element price = document.createElement("PRICE");
Element year = document.createElement("YEAR");


title.appendChild(document.createTextNode("mike "));
artist.appendChild(document.createTextNode("oconnor "));
country.appendChild(document.createTextNode("ie "));
company.appendChild(document.createTextNode("dell "));
price.appendChild(document.createTextNode("14 "));
year.appendChild(document.createTextNode("2014 "));

cd.appendChild(title);
cd.appendChild(artist);
cd.appendChild(country);
cd.appendChild(company);
cd.appendChild(price);
cd.appendChild(year);
}

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File("cdCatalog.xml"));
transformer.transform(domSource, streamResult);

DOMSource source = new DOMSource(document);

}

我可以创建一个新的CD元素,但新元素出现在我的根 ( <CATALOG> ) 元素之外:

<?xml version="1.0" encoding="utf-8" standalone="no"?><CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG><CD><TITLE>mike </TITLE><ARTIST>oconnor </ARTIST><COUNTRY>ie </COUNTRY><COMPANY>dell </COMPANY><PRICE>14 </PRICE><YEAR>2014 </YEAR><TITLE>mike </TITLE><ARTIST>oconnor </ARTIST><COUNTRY>ie </COUNTRY><COMPANY>dell </COMPANY><PRICE>14 </PRICE><YEAR>2014 </YEAR><TITLE>mike </TITLE><ARTIST>oconnor </ARTIST><COUNTRY>ie </COUNTRY><COMPANY>dell </COMPANY><PRICE>14 </PRICE><YEAR>2014 </YEAR></CD>

我认为我附加了错误的元素,但不知道如何附加到 CATALOG .

最佳答案

问题是,您是按文档追加元素,您需要按根元素追加 CD

尝试这样:

...
Document document = db.parse(xml);
Element root = document.getDocumentElement();
Element cd = document.createElement("CD");
root.appendChild(cd);
.....
......
....

关于java - 使用 Java DOM 将新元素添加到 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22413805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com