EDIT: Problem Found - I was adding the elements to doc, when it should have been added to "rootElement." Working now. If you guys have any further suggestions as to how to make my code better, please let me know
In the following code, I try to both manually make an entry into an XML file, and then to streamline it a little bit with a function.
I'm getting the error "HIERARCHY_REQUEST_ERR". I know that the problem is from the following code towards the bottom in my function, but I don't know how to deal with it. When I get rid of doc.appendChild(staff);, I get rid of the problem, but it obviously doesn't add the new entry.
Element staff = doc.createElement("Staff"); doc.appendChild(staff);
Thanks for looking!!
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);
}
}
Change this function to:
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);
}
And create elements like:
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");