問題描述
以下代碼在 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:
- 創建一個新的 maven 項目,使用我在下面附加的那段簡單的代碼,程序正常崩潰并出現錯誤,無法解析結構,這沒關系.
將你的依賴項復制到項目 pom.xml 中,現在程序應該會崩潰(如上所述)
- 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.
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.
現在你可以決定要做什么了,也許有新版本可用,在我們的例子中,它是一個學院的自己的包,他包含一個學院的包,我可以排除.
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 類
and the testXML class
@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: In my case it was
<dependency>
<groupId>jcs</groupId>
<artifactId>jcs</artifactId>
<version>1.3</version>
</dependency>
希望對您有所幫助.
這篇關于在 java-8 “secure-processing org.xml.sax.SAXNotRecognizedException 導致 java.lang.IllegalStateException"中解組 xml 時出錯的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!