XML parsing/writing to file with DocType
Posted on 2004-04-29
I am parsing a web.xml document and setting some values then writing the file back to the file system. When I write the document to the file system the DocType remote URL dtd is being resolved and content inserted into the resulting file. All I want is to parse the xml set a couple values and write the xml back to file with the DocType preserved instead of being resolved and content included. Below is my code snipet.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File(new File(workDirectory, "WEB-INF"), "web.xml"));
//Set a couple values...
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(new File (workDirectory, "WEB-INF"), "web.xml")));
StreamResult result = new StreamResult(out);
I have tried to set the TranformerFactory.setURIResolver(null) with no change in the result and am not sure what this is supposed to do. I also used the root document node when constructiong the DOMSource but that just removes the DocType from the resulting xml and I want to maintain the original.
The parser being used is Xerces.