本文介绍了xml 1.1 在 xsd 中使用时导致 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xsd 文件.我正在尝试使用该 xsd 文件创建 javax.xml.validation.Schema.我写了一个程序来做到这一点.当我使用 <?xml version="1.0" encoding="UTF-8"?> 时,一切正常.但是当我使用 <?xml version="1.1" encoding="UTF-8"?> 然后第一次尝试创建 Schema 是抛出 NPE 但第二次和随后的尝试都成功了.以下是来源:

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");
    }
}

首先执行的方法(doIt()doItAgain())正在抛出 NPE.

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

下面是堆栈跟踪:

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)

我的问题是,为什么 XML 1.1 不能正常工作?为什么只有第一次尝试不成功?

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

推荐答案

我发现如果我使用 Xerces jar 的最新版本,我不会遇到问题.我还发现问题是因为 JDK 本身存在一个错误,它也记录在 openjdk网站 更有趣的是,没有人关心解决这个问题.因此,请使用最新版本的 Xerces jar 或使用 xml 版本 1.0.我会选择第一个选项.谁知道旧的会有多少隐藏的惊喜:P

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

这篇关于xml 1.1 在 xsd 中使用时导致 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 10:10