2

我有一个这样的 XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="www.aaa.com/aa" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:include schemaLocation="class.xsd"/>
    <xs:element name="record">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Stud" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="fname" type="xs:string"/>
                            <xs:element name="lname" type="xs:string"/> 
                            ........                                
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

当我使用 XSOM 解析这个 XSD 时,它给出了Null Pointer Exception. 当我删除<xs:include schemaLocation="class.xsd"/>元素时它工作正常

 XSOMParser parser = new XSOMParser();
       parser.parse(inputStream);// -> this statement gives null pointer.

堆栈跟踪

Exception in thread "main" java.lang.NullPointerException
    at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.parseEntity(NGCCRuntimeEx.java:327)
    at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.includeSchema(NGCCRuntimeEx.java:234)
    at com.sun.xml.xsom.impl.parser.state.includeDecl.action0(includeDecl.java:42)
    at com.sun.xml.xsom.impl.parser.state.includeDecl.leaveElement(includeDecl.java:109)
    at com.sun.xml.xsom.impl.parser.state.NGCCRuntime.endElement(NGCCRuntime.java:275)
    at org.xml.sax.helpers.XMLFilterImpl.endElement(XMLFilterImpl.java:546)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
    at org.apache.xerces.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
    at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at org.xml.sax.helpers.XMLFilterImpl.parse(XMLFilterImpl.java:333)
    at com.sun.xml.xsom.parser.JAXPParser.parse(JAXPParser.java:115)
    at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.parseEntity(NGCCRuntimeEx.java:337)
    at com.sun.xml.xsom.impl.parser.ParserContext.parse(ParserContext.java:124)
    at com.sun.xml.xsom.parser.XSOMParser.parse(XSOMParser.java:183)
    at com.sun.xml.xsom.parser.XSOMParser.parse(XSOMParser.java:138)

提前致谢

4

1 回答 1

4

为什么会发生 NullPointerException 问题是没有错误处理程序来处理解析时抛出的异常,因此解析中的异常会导致返回 null。

将实现的实例设置org.xml.sax.ErrorHandler为 errorHandler 解决了这个问题。

parser.setErrorHandler(new DOMErrorHandler());

DOMErrorHandler是org.xml.sax.ErrorHandler 的实现。

为什么 xs:include 不起作用

The parser parses the InputStream of the specified XSD. So it won't get the systemId() of given schema, using which a baseURI has to be created for accessing included files. Thus it won't be able to access the file to be included.

So when i call parser.parse() with InputSource of the schema, it works fine.

于 2013-01-02T07:13:59.343 回答