gpt4 book ai didi

java - SAXUnmarshaller java.lang.NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-04 14:27:56 25 4
gpt4 key购买 nike

我在解码 xml 内容时遇到以下异常。我在 weblogic 12c 下运行,并在 war 中打包了 xercerimpl.jar 和 xalan.jar jar。请告知此错误可能是什么问题。

Exception Description: An error occurred while trying to instantiate the schema platform.
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.exceptions.XMLMarshalException.errorInstantiatingSchemaPlatform(XMLMarshalException.java:171)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.getXMLReader(SAXUnmarshaller.java:173)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:333)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:320)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:619)
at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:408)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:149)
... 73 more
Caused by: java.lang.NullPointerException
at org.apache.xerces.parsers.AbstractSAXParser.setEntityResolver(AbstractSAXParser.java:1177)
at org.eclipse.persistence.internal.oxm.record.XMLReader.setEntityResolver(XMLReader.java:86)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.getXMLReader(SAXUnmarshaller.java:166

最佳答案

您可以尝试以下方法作为解决方法,以在正确的位置获取 EntityResolver:

import java.io.IOException;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBHelper;
import org.eclipse.persistence.jaxb.JAXBUnmarshaller;
import org.eclipse.persistence.oxm.XMLUnmarshaller;
import org.xml.sax.EntityResolver;
import org.xml.sax.*;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Demo.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBUnmarshaller jaxbUnmarshaller = JAXBHelper.getUnmarshaller(unmarshaller);
XMLUnmarshaller xmlUnmarshaller = jaxbUnmarshaller.getXMLUnmarshaller();
xmlUnmarshaller.setEntityResolver(new EntityResolver() {

@Override
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
return null;
}

});
}

}
<小时/>

I am running under weblogic 12c and have xercerimpl.jar & xalan.jar jars packaged inside the war.

为什么你的war中包含xerceimpl.jar和xalan.jar?

Those jars are added by the component into my war project by Maven.

我建议在您的 Maven 设置中修复此问题。

<小时/>

更新

The unmarschalling logic is written in seperate component which they can't change it (works for other projects). Is there away i can fix it in my side (inside web project)? Here is the component code, JAXBContext jc = JAXBContext.newInstance(Config.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); Result result = (Config) unmarshaller.unmarshal(url);

尝试以下操作,这将导致 MOXy 走上一条利用 StAX 解析器而不是 SAX 解析器的路径:

InputStream inputStream = url.openStream;
Result result = (Config) unmarshaller.unmarshal(inputStream);
inputStream.close();

关于java - SAXUnmarshaller java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26468124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com