Click to See Complete Forum and Search --> : problem: when writing out an XML document, DTD is always included


vmmattil
02-16-2005, 05:53 AM
My problem is, I use javax.xml.Transform to write out a modified Document object to a file. It has a dtd, and it is included every time in the written file, no matter what. Why? How do I get rid of the behaviour?

Here's the code:

try {
// Create a transformer
Transformer xformer = TransformerFactory.newInstance()
.newTransformer();

// Write the DOM document to a file
Source source = new DOMSource(doc);
File newFile = new File(foo.xml);
Result result = new StreamResult(newFile);
xformer.transform(source, result);
} catch (TransformerConfigurationException e) {
System.out.println(e.getMessage());
} catch (TransformerException e) {
System.out.println(e.getMessage());
}

Khalid Ali
02-16-2005, 11:33 PM
if the DOCTYPE element is already in the file that you read and then print, you should be able to delete that particular element for the document and then push to file.???

vmmattil
02-17-2005, 07:00 AM
Originally posted by Khalid Ali
if the DOCTYPE element is already in the file that you read and then print, you should be able to delete that particular element for the document and then push to file.???

Thanks, Khalid. It works if I manually remove the DOCTYPE definition. I find it hard to delete that element programmatically, however. I haven't checked which DOM level I am using, but I noticed that DOM level 1 doesn't support modifying the DOCTYPE.

br,
Marcus

Khalid Ali
02-17-2005, 09:24 AM
Sounds strange, have u tried usingElement.getElementsByTagName("DOCTYPE");
the above will return a NodeList, go through the list (ofcourse there should be only 1 element in the list)
and then use the following method to remove the child
Element.removeChild(Node);
Let me know what are your findings..