Thursday, October 15, 2009

Create XML file in java

This is a simple example of creating standard XML file in java.

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("root");

This code segment creates a root element of the XML file.

Now, use this code to create child nodes to XML document.

Element em = document.createElement("element");
em.appendChild(document.createTextNode("data"));
rootElement.appendChild(em);

No comments:

Post a Comment