gpt4 book ai didi

java - 尝试在 Java 中创建 XML 文件 - 获取 DOMException : HIERARCHY_REQUEST_ERR

转载 作者:行者123 更新时间:2023-12-01 12:41:35 28 4
gpt4 key购买 nike

编辑:发现问题 - 我正在将元素添加到文档中,而它本应添加到“rootElement”中。现在工作了。如果你们对如何改进我的代码有任何进一步的建议,请告诉我

在下面的代码中,我尝试手动在 XML 文件中输入一个条目,然后使用函数对其进行稍微简化。

我收到错误“HIERARCHY_REQUEST_ERR”。我知道问题出在我的函数底部的以下代码中,但我不知道如何处理它。当我摆脱 doc.appendChild(staff); 时,我摆脱了问题,但它显然没有添加新条目。

Element Staff = doc.createElement("Staff");doc.appendChild(staff);

感谢您的浏览!!

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class createXMLfile {

public static void main(String [] args) throws ParserConfigurationException, TransformerException{

String address = "/home/leor/workspace/Test/Files/src/outputFile.xml";


DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();

Element rootElement = doc.createElement("Company");
doc.appendChild(rootElement);

// EMPLOYEE #1

//-----------------------------------------------------------//


Element staff = doc.createElement("Staff");
rootElement.appendChild(staff);

staff.setAttribute("id", "1");

Element firstName = doc.createElement("First_Name");
firstName.appendChild(doc.createTextNode("John"));
staff.appendChild(firstName);

Element lastName = doc.createElement("Last_Name");
lastName.appendChild(doc.createTextNode("Smith"));
staff.appendChild(lastName);

Element salary = doc.createElement("Salary");
salary.appendChild(doc.createTextNode("120,000"));
staff.appendChild(salary);

//-----------------------------------------------------------//

createNewEntry(doc, "2", "Leo", "Benner", "90,000");
createNewEntry(doc, "3", "Sarah", "Gordon", "75,000");
createNewEntry(doc, "4", "Scott", "Jones", "55,000");

//-----------------------------------------------------------//

TransformerFactory f = TransformerFactory.newInstance();
Transformer tf = f.newTransformer();
DOMSource source = new DOMSource(doc);

StreamResult r = new StreamResult(new File(address));

tf.transform(source, r);

System.out.println("File created.");

}

//-----------------------------------------------------------//


public static void createNewEntry(Document doc, String id, String fName, String lName, String sal){

Element staff = doc.createElement("Staff");
doc.appendChild(staff);

staff.setAttribute("id", id);

Element firstName = doc.createElement("First_Name");
firstName.appendChild(doc.createTextNode(fName));
staff.appendChild(firstName);

Element lastName = doc.createElement("Last_Name");
lastName.appendChild(doc.createTextNode(lName));
staff.appendChild(lastName);

Element salary = doc.createElement("Salary");
salary.appendChild(doc.createTextNode(sal));
staff.appendChild(salary);

}

}

最佳答案

将此函数更改为:

public static void createNewEntry(Document doc, Element root, String id, String fName, String lName, String sal){

Element staff = doc.createElement("Staff");
root.appendChild(staff);

staff.setAttribute("id", id);

Element firstName = doc.createElement("First_Name");
firstName.appendChild(doc.createTextNode(fName));
staff.appendChild(firstName);

Element lastName = doc.createElement("Last_Name");
lastName.appendChild(doc.createTextNode(lName));
staff.appendChild(lastName);

Element salary = doc.createElement("Salary");
salary.appendChild(doc.createTextNode(sal));
staff.appendChild(salary);

}

并创建如下元素:

Element rootElement = doc.createElement("Company");
doc.appendChild(rootElement);


createNewEntry(doc, rootElement, "1", "John", "Smith", "120,000");
createNewEntry(doc, rootElement, "2", "Leo", "Benner", "90,000");
createNewEntry(doc, rootElement, "3", "Sarah", "Gordon", "75,000");
createNewEntry(doc, rootElement, "4", "Scott", "Jones", "55,000");

关于java - 尝试在 Java 中创建 XML 文件 - 获取 DOMException : HIERARCHY_REQUEST_ERR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25048782/

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