在 Java 6 中解析 XSD 的旧代码在 Java 7 中失败。原因归结为属性 http://www.oracle.com/xml/jaxp/properties/xmlSecurityPropertyManager未设置,这会导致 NullPointerException。代码:
System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl");
DOMImplementationRegistry registry = newInstance();
XSImplementation impl = (XSImplementation) registry.getDOMImplementation("XS-Loader");
XSLoader schemaLoader = impl.createXSLoader(null);
String url = SchemaParser.class.getResource("/some.xsd").toURI().toString();
XSModel model = schemaLoader.loadURI(url);
NPE 发生在 loadUri 类 XSDHandler.reset() 中:
XMLSecurityPropertyManager securityPropertyMgr = (XMLSecurityPropertyManager)
componentManager.getProperty(XML_SECURITY_PROPERTY_MANAGER);
//Passing on the setting to the parser
fSchemaParser.setProperty(XML_SECURITY_PROPERTY_MANAGER, securityPropertyMgr);;
securityPropertyMgr,setProperty() 抛出 NPE。我怎样才能解决这个问题?
最佳答案
他们使用 == 比较。我不得不使用常量:
((XSLoaderImpl)loader).setParameter(
com.sun.org.apache.xerces.internal.impl.Constants.XML_SECURITY_PROPERTY_MANAGER,
new XMLSecurityPropertyManager());
参见:http://osdir.com/ml/core-libs-dev/2013-08/msg00260.html但它是针对 JDK 8 的,我希望我们能在 7 中得到修复,但我没有成功发布它的错误。
关于Java 7 http ://www. oracle.com/xml/jaxp/properties/xmlSecurityPropertyManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19201185/