在 Java 中将 XML 文档写入文件时出错
我正在尝试将 org.w3c.dom.Document 写入文件。 获取
String URL = "http://...."
DOMParser parser = new DOMParser();
Document doc = null;
try {
parser.parse(new InputSource(URL));
doc = parser.getDocument();
} catch () {}
我从然后我将此 Document
写入文件中
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(file));
transformer.transform(source, result);
Document
在执行此操作时,我不断收到以下错误
ERROR: 'Namespace for prefix 'xlink' has not been declared.'
可能出了什么问题?谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用不同的库,例如 Dom4J,而不是尝试通过 Java 中的内置 XML API 来解决问题。 Dom4J 设计得更好,使您的代码更具可读性:
这些都不会与
FactoryFactoryFactoryFactories
混在一起。我知道这并不能直接回答您的问题,但希望它无论如何都会有所帮助。 Dom4j 知道如何与 Java XML API 通信,因此您可以混合搭配它们以满足您的需求。如果您想使用 XSLT,您甚至可以将其插入 Xalan 或类似的东西中。
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:
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.