/*
* Create_DOM.java
*
* Created on March 24, 2008, 4:28 PM
*
* Created by : Kaustubh Verma
www.campusfever.wordpress.com
www.javagurug.blogspot.com
* Created on March 24, 2008, 3:54 PM
*
* Purpose : To demostrate the insertion of root node BOOKS
to insert text node: this is an XML file
and save it in "book.xml"
*/
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
// import dom.Element for root node
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class Create_DOM {
private Document document = null;
/** Creates a new instance of Create_DOM */
public Create_DOM() {
//
DocumentBuilder builder = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try{
builder = factory.newDocumentBuilder();
document = builder.newDocument();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
// Insert Root Order
Element root = (Element) document.createElement("BOOKS");
document.appendChild(root);
// Insert a text node
Node bookChild = document.createTextNode("this is an xml file ");
root.appendChild(bookChild);
// Normalizing the DOM
document.getDocumentElement().normalize();
}
//
public Document getDocument(){
return document;
}
public static void main(String ...args){
Document document = null;
// Build new DOM Document
Create_DOM cDOM = new Create_DOM();
document = cDOM.getDocument();
// Print XML content
DOMUtil.printDOM(document);
// Write to a file, give the name of OUTPUT XML FILE
DOMUtil.writeXmlToFile("book.xml",document);
}
}
No comments:
Post a Comment