"jave write xml file" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "jave write xml file" answered properly. Developers are finding an appropriate answer about jave write xml file related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like jave write xml file. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on jave write xml file.
jave write xml file
package com.javacodegeeks.java.core;
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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CreateXMLFileJava {
public static final String xmlFilePath = "C:\\Users\\nikos7\\Desktop\\files\\xmlfile.xml";
public static void main(String argv[]) {
try {
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
// root element
Element root = document.createElement("company");
document.appendChild(root);
// employee element
Element employee = document.createElement("employee");
root.appendChild(employee);
// set an attribute to staff element
Attr attr = document.createAttribute("id");
attr.setValue("10");
employee.setAttributeNode(attr);
//you can also use staff.setAttribute("id", "1") for this
// firstname element
Element firstName = document.createElement("firstname");
firstName.appendChild(document.createTextNode("James"));
employee.appendChild(firstName);
// lastname element
Element lastname = document.createElement("lastname");
lastname.appendChild(document.createTextNode("Harley"));
employee.appendChild(lastname);
// email element
Element email = document.createElement("email");
email.appendChild(document.createTextNode("[email protected]"));
employee.appendChild(email);
// department elements
Element department = document.createElement("department");
department.appendChild(document.createTextNode("Human Resources"));
employee.appendChild(department);
// create the xml file
//transform the DOM Object to an XML File
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File(xmlFilePath));
// If you use
// StreamResult result = new StreamResult(System.out);
// the output will be pushed to the standard output ...
// You can use that for debugging
transformer.transform(domSource, streamResult);
System.out.println("Done creating XML File");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}
Source: examples.javacodegeeks.com
All those coders who are working on the Whatever based application and are stuck on jave write xml file can get a collection of related answers to their query. Programmers need to enter their query on jave write xml file related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about jave write xml file for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of jave write xml file while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "jave write xml file". Visit this developer's friendly online web community, CodeProZone, and get your queries like jave write xml file resolved professionally and stay updated to the latest Whatever updates.