使用 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。我怎样才能解决这个问题?