Feb 10

A Transformer can be used to write a the XML for a DOM Document to a file. As the name implies a Transformer transforms a Source object into a Result, in this case the Source is the DOM Document, and the Result is an XML file.

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
Result result = new StreamResult(new File(xmlOutputFilePath));
Source source = new DOMSource(document);
transformer.transform(source, result);

written by objects \\ tags: , ,

Nov 10

DocumentBuilderFactory factory = 
   DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file); 
// or if you need to specify an encoding explitily
// Document document = builder.parse(
//   new InputStreamReader(new FileInputStream(file), encoding)); 

written by objects \\ tags: , , ,

Nov 10

You need to open an InputStream for the URL and pass that to your DocumentBuilder

DocumentBuilderFactory factory = 
   DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(url.openStream()); 

written by objects \\ tags: , , ,