编辑:发现问题 - 我将这些元素添加到doc,当它应该被添加到“rootElement”时。现在工作。如果您有任何关于如何使我的代码更好的进一步建议,请告诉我
在下面的代码中,我尝试手动输入XML文件,然后使用函数对其进行简化。
我收到错误“HIERARCHY_REQUEST_ERR”。我知道问题是从我的函数中的下面的代码到底部,但我不知道如何处理它。当我摆脱doc.appendChild(staff);时,我摆脱了问题,但显然没有添加新条目。
元素staff = doc.createElement(“Staff”); doc.appendChild(人员);
感谢您的期待!!
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);
}
}
答案 0 :(得分:1)
将此功能更改为:
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");