xerces dtd解析验证问题

xerces解析xml文档默认会验证dtd,这个过程需要网络连通,而且可能非常耗时。同时使用xerces的包又是很多的,这个设计真是麻烦!

对于dom4j,可以这样解决:

SAXReader reader = new SAXReader();
	
			reader.setValidation(false);
			try
			{
				reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
			}
			catch (SAXException e)
			{
				// do nothing
			}
		

对于javax.xml:

javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory
					.newInstance();
			documentBuilderFactory.setValidating(false);
			javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			documentBuilder.setEntityResolver(new EntityResolver() {
				public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
				{
					return new org.xml.sax.InputSource(new java.io.StringReader(""));
				}
			});

其他类似,设置自己的EntryResolver即可。


Total views.

© 2013 - 2024. All rights reserved.

Powered by Hydejack v6.6.1