xml 1.1 causing NullPointerException when used in xsd

試著忘記壹切 提交于 2019-12-11 18:25:55

问题


I have an xsd file. I am trying to create javax.xml.validation.Schema using that xsd file. I have written a program that does it. When I use <?xml version="1.0" encoding="UTF-8"?> every thing works fine. But when I use <?xml version="1.1" encoding="UTF-8"?> then the first attempt to create the Schema is throwing NPE but the second and subsequent attempts are successful. Below is the source:

import java.io.File;

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

public class Test {
    SchemaFactory sf = null;

    public static void main(String args[]) {

        Test test = new Test();
        test.init();
        File schemaFile = new File("D:\\test.xsd");

        try {
            test.doIt(schemaFile);
        } catch (Exception e) {
            System.out.println("doIt() failed " + e.getMessage());
        }
        try {
            test.doItAgain(schemaFile);
        } catch (Exception e) {
            System.out.println("doItAgain() failed " + e.getMessage());
        }

        System.out.println("Execution completed");

    }

    public void doIt(File schemaFile) throws Exception {
        @SuppressWarnings("unused")
        Schema schema = null;
        synchronized (sf) {
            schema = sf.newSchema(schemaFile);
        }
        System.out.println("doIt() success");
    }

    public void doItAgain(File schemaFile) throws Exception {
        @SuppressWarnings("unused")
        Schema schema = null;

        synchronized (sf) {
            schema = sf.newSchema(schemaFile);
        }
        System.out.println("doAgainIt() success");
    }

    public void init() {
        sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    }
}

The method (doIt() or doItAgain()) executed first is throwing NPE.

Below is the stack trace:

java.lang.NullPointerException
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1738)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1770)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipSpaces(XMLEntityScanner.java:1543)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$TrailingMiscDriver.next(XMLDocumentScannerImpl.java:1400)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaParsingConfig.parse(SchemaParsingConfig.java:435)
    at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaParsingConfig.parse(SchemaParsingConfig.java:491)
    at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaDOMParser.parse(SchemaDOMParser.java:510)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument(XSDHandler.java:1802)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:531)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:552)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:519)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar(XMLSchemaLoader.java:485)
    at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:210)
    at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:594)
    at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:610)
    at Test.doIt(Test.java:39)
    at Test.main(Test.java:20)

My question is, Why XML 1.1 is not working properly? Why only first attempt is unsuccessful?


回答1:


I found that if I use the latest versio of Xerces jar I dont face the isue. I also found that the problem is because there is a bug in JDK itself and its also logged at openjdk website and more interesting is that no one cared to fix this. So, use latest version of Xerces jar or use xml version 1.0. I will go with the first option. Who knows how many hidden surprises would be there in the old one :P



来源:https://stackoverflow.com/questions/24405840/xml-1-1-causing-nullpointerexception-when-used-in-xsd

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