How do I insert an internal dtd into an xml file using Java
How do I insert an internal dtd into an xml file using Java
I have the following test code which reads in an XML file into a DOM Document, then writes it out again. I would like the output files to end up being the same as the input file.
public class DomToXml {
Document document;
void process() {
document = parseXmlFile("dat/input.xml");
writeXmlFile(document, "dat/output.xml");
}
void writeXmlFile(Document document, String fileName) {
try {
Source source = new DOMSource(document);
File file = new File(fileName);
Result result = new StreamResult(file);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
DocumentType documentType = document.getDoctype();
if (documentType != null) {
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, documentType.getSystemId());
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, documentType.getPublicId());
}
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
Document parseXmlFile(String filename) {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setXIncludeAware(true);
documentBuilderFactory.setExpandEntityReferences(true);
documentBuilderFactory.setValidating(false);
DocumentBuilder documentBuilder;
try {
documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilder.setErrorHandler(new MyErrorHandler());
documentBuilder.setEntityResolver(new MyEntityResolver());
return documentBuilder.parse("file:" + filename);
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
return null;
}
public static void main(String[] args) {
DomToXml a = new DomToXml();
a.process();
System.out.println("Done!");
}
}
The Input file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE root PUBLIC "NONE"
"none.dtd" [
<!ENTITY foo "bar">
]>
<root id="TEMPLATE" xreflabel="TEMPLATE-&foo;">
</root>
The output:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE root PUBLIC "NONE" "none.dtd">
<root id="TEMPLATE" xreflabel="TEMPLATE-bar">
</root>
As you can see, the dtd along with the entity information is not being outputed. How do i fix this?
As a secondary question, how do I stop the transformation of the entity. i.e. I want the output file to contain TEMPLATE开发者_开发知识库-&foo; rather than TEMPLATE-bar.
Thanks,
Wayne.
I can get the DTD entity declaration in the output, but haven't worked out how to preserve the entity itself:
DOMImplementationLS domImplementation = (DOMImplementationLS) DOMImplementationRegistry
.newInstance().getDOMImplementation("LS");
LSSerializer lsSerializer = domImplementation.createLSSerializer();
System.out.println(lsSerializer.writeToString(document));
The simple way for converting String to DOM and vice versa is probably presents here:
http://famulatus.com/ks/programming/java/item/335-convert-xml-dom-to-string-and-string-to-xml-dom.html
精彩评论