Java更改并移动非标准XML文件 [英] Java change and move non-standard XML file
问题描述
我正在使用第三方应用程序,并希望更改其中的一个文件。该文件存储在XML中,但是使用无效的doctype。
I am using a third party application and would like to change one of its files. The file is stored in XML but with an invalid doctype.
当我尝试读取它使用错误时,因为doctype包含file:///ReportWiz.dtd
(如显示,带引号),我收到一个找不到文件的异常。有没有办法告诉docbuilder忽略这个?对于DocumentBuilderFactory,我已经将setValidate设为false,将setNamespaceAware设置为false。
When I try to read use a it errors out becuase the doctype contains "file:///ReportWiz.dtd" (as shown, with quotes) and I get an exception for cannot find file. Is there a way to tell the docbuilder to ignore this? I have tried setValidate to false and setNamespaceAware to false for the DocumentBuilderFactory.
我可以想到的唯一解决方案是
The only solutions I can think of are
- 逐行复制文件进入一个新文件,省略违规行,做我需要做的事情,然后复制到另一个新文件中并插入违规行,或者
- 做的大致相同,但工作有一些FileStream(虽然我不清楚我该怎么做这个..help?)
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
docFactory.setValidating(false);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(file);
推荐答案
告诉您的DocumentBuilderFactory忽略DTD声明像这样:
Tell your DocumentBuilderFactory to ignore the DTD declaration like this:
docFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
请参阅这里列出了可用的功能。
See here for a list of available features.
您还可能会发现JDOM比org更容易使用.w3c.dom:
You also might find JDOM a lot easier to work with than org.w3c.dom:
org.jdom.input.SAXBuilder builder = new SAXBuilder();
builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
org.jdom.Document doc = builder.build(file);
这篇关于Java更改并移动非标准XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!