Thursday, February 26, 2009

XML, DOM, JAVA

One of those things that I never bother remembering the exact details of, but end up having to look up on the internet all the time.

Note to self: XML, DOM, JAVA
Taken from this guys site: http://www.genedavis.com/library/xml/java_dom_xml_creation.jsp
Also a bit from here: http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html

// get dom doc
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
// Document doc = docBuilder.parse(new File(inputFilename));

// -- insert building dom here

// get dom transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
// make dom into something useful
FileWriter fw = new FileWriter(new File(outputFilename));
StreamResult result = new StreamResult(fw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);

No comments: