简体   繁体   中英

Error writing XML Document to file in Java

I am trying to write org.w3c.dom.Document to a file. I get the Document from

String URL = "http://...."
DOMParser parser = new DOMParser();
Document doc = null;
try {
    parser.parse(new InputSource(URL));
    doc = parser.getDocument();
} catch () {}

Then I write this Document to a file using

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(file));
transformer.transform(source, result);

While doing this I keep getting the following error

ERROR:  'Namespace for prefix 'xlink' has not been declared.'

What might be wrong? Thanks

I recommend using a different library such as Dom4J rather than trying to fight your way through the built-in XML API in Java. Dom4J is better designed and makes your code much more readable:

Document doc = new SAXReader().read(inputStream);
new XMLWriter(outputStream).write(doc);

None of this mucking around with FactoryFactoryFactoryFactories .

I know this doesn't directly answer your question but hopefully it will help anyway. Dom4j knows how to talk to the Java XML API so you can mix and match them to suit your needs. You can even plug it into Xalan or something similar if you want to use XSLT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM