开发者

Error writing XML Document to file in Java

开发者 https://www.devze.com 2023-03-23 17:12 出处:网络
I am trying to write org.w3c.dom.Document to a file. I get the Document from String URL = \"http://....\"

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开发者_StackOverflow社区 () {}

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消