在java-8中解组xml时出错“安全处理org.xml.sax.SAXNotRecognizedException导致java.lang.IllegalStateException” [英] Error unmarshalling xml in java-8 "secure-processing org.xml.sax.SAXNotRecognizedException causing java.lang.IllegalStateException"

查看:348
本文介绍了在java-8中解组xml时出错“安全处理org.xml.sax.SAXNotRecognizedException导致java.lang.IllegalStateException”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在Java 7中运行良好

The following code worked fine in Java 7

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

String xmlString = '<xml ..... ';

StringReader reader = new StringReader(xmlString);

JAXBContext jc = JAXBContext.newInstance(MyClass.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
MyClass myClass = (MyClass) unmarshaller.unmarshal(reader);
....

现在我们不得不升级到Java 8,现在我得到了这个执行代码时的异常:

Now we had to upgrade to Java 8 and now I get this exception when executing the code:

Sep 03, 2014 1:42:47 PM com.sun.xml.internal.bind.v2.util.XmlFactory createParserFactory
SCHWERWIEGEND: null
org.xml.sax.SAXNotRecognizedException: Feature: http://javax.xml.XMLConstants/feature/secure-processing
    at org.apache.xerces.jaxp.SAXParserFactoryImpl.setFeature(SAXParserFactoryImpl.java:100)
    at com.sun.xml.internal.bind.v2.util.XmlFactory.createParserFactory(XmlFactory.java:114)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.getXMLReader(UnmarshallerImpl.java:139)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)

我知道有一个问题针对类似问题,但退回到java 7对我来说不是解决方案。

I know that there is a question targeting a similar problem, but stepping back to java 7 is not a solution for me.

我试图添加以下maven依赖

I tried to add the following maven dependency

<dependency>
    <groupId>javax.xml</groupId>
    <artifactId>jaxp-api</artifactId>
    <version>1.4</version>
</dependency>

但这并未改变结果,所以我删除了它(感谢@BlaiseDoughan提供的信息,这包含在Java 6中)

but that did not change the result, so I removed it (thanks to @BlaiseDoughan for the information, that this is included in Java 6)

欢迎任何提示,非常感谢。

Any hints are welcome, many thanks.

推荐答案

这是一个依赖性问题。

以下是我解决问题的方法:

Here is my way how I solved the problem:


  1. 创建一个新的maven项目,使用下面附带的简单代码片段,程序正常崩溃并出现错误,结构无法解析,这是正常的。

  2. 将您的依赖项复制到项目pom.xml中,现在程序崩溃了(如上所述)

  1. Make a new maven project, with that simple pieces of code, I attached below, the programm crashed normally with an error, that the structure couldn't be parsed which is ok.
  2. Copy your dependencies into the project pom.xml, now the programm should crash (as described above)

不,你在您喜欢的方法(良好的猜测,Bisection,1-by-1 ..)之后删除依赖关系以找到坏依赖关系。也许有人有一个更好(更专业)的方法,这个方法对我有效。

no you remove dependencies after your favoured method (good guessing, Bisection , 1-by-1 ..) to find the "bad" dependency. Maybe someone has a better (more professional) method, this one worked for me.

现在你可以决定做什么do,也许是新版本可用,在我们的情况下,它是一个大学的自己的包,在那里他包括一个大学的包,我可以排除。

now you can descide what to do, maybe a new version is available, in our case it was out own package of a colleage, where he included a package of a colleage, which i could exclude.

public class Test {
    public Test() {
    }
    public static void main(String[] args) {
        try {
            StringReader reader = new StringReader("<xml></xml>");
            JAXBContext jc = JAXBContext.newInstance(TestXML.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            TestXML testXMLs = (TestXML) unmarshaller.unmarshal(reader);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

和testXML类

@XmlRootElement(name="rss")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestXML {   
    public TestXML() {
    }

    @XmlElementWrapper(name="channel")
    @XmlElement(name="item")
    private int i ;

    public int getI() {
        return i;
    }    
    public void setI(int i) {
        this.i = i;
    }
}

BTW:在我的情况下是

BTW: In my case it was

<dependency>
    <groupId>jcs</groupId>
    <artifactId>jcs</artifactId>
    <version>1.3</version>
</dependency>

希望有所帮助。

这篇关于在java-8中解组xml时出错“安全处理org.xml.sax.SAXNotRecognizedException导致java.lang.IllegalStateException”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆