StAX - how to set XMLInputFactory.IS_VALIDATING to true?

偶尔善良 提交于 2019-12-10 14:51:06

问题


this is my first time using StAX for parsing XML documents (still in the learning stage). During the process to parse an XML document using XMLStreamReader and generate a copy of the document using XMLStreamWriter, I encountered the following warning represented as a comment in the output of the writer:

 <!-- Exception scanning External DTD Subset.  True contents of DTD cannot be determined.  Processing will continue as XMLInputFactory.IS_VALIDATING == false. -->

I understood the cause of the warning, but I wanted it to become an error rather than silently became a warning, so then I tried to set XMLInputFactory.IS_VALIDATING to true:

 XMLInputFactory factory = XMLInputFactory.newInstance();
 factory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.TRUE);

But the above threw an exception for me:

Exception in thread "main" java.lang.IllegalArgumentException: true value of isValidating not supported at com.sun.org.apache.xerces.internal.impl.PropertyManager.setProperty(PropertyManager.java:150) at com.sun.xml.internal.stream.XMLInputFactoryImpl.setProperty(XMLInputFactoryImpl.java:257) at com.test.test2.helper.SgmlDocumentParser.parse(SgmlDocumentParser.java:83) at com.test.test2.helper.Test.main(Test.java:66)

So what is wrong with my approach?

Thanks!


回答1:


It looks like Sun's StAX implementation just doesn't support DTD validation. You could try using Woodstox instead, it appears to support validation.



来源:https://stackoverflow.com/questions/1659094/stax-how-to-set-xmlinputfactory-is-validating-to-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!