Error unmarshalling xml in java-8 “secure-processing org.xml.sax.SAXNotRecognizedException causing java.lang.IllegalStateException”

后端 未结 11 450
时光取名叫无心
时光取名叫无心 2020-12-05 00:21

The following code worked fine in Java 7

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

String          


        
相关标签:
11条回答
  • 2020-12-05 01:09

    Try to create a XML Document and unmarshal it. It was worked for me. JAXBContext jc = JAXBContext.newInstance( Message.class );

            InputStream stream = new ByteArrayInputStream( string.getBytes( StandardCharsets.UTF_8 ) );
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse( stream );
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Message msg = ( Message ) unmarshaller.unmarshal( doc );
    
    0 讨论(0)
  • 2020-12-05 01:10

    Both Bernard and Blaise's answers were very helpful. In my case, since I am using JDK 7, the solution was to exclude the xerces subdependency that was being included by one of my dependencies:

    <dependency>
      <groupId>org.apache.axis</groupId>
      <artifactId>axis</artifactId>
      <version>1.4.1-SNAPSHOT</version>
      <exclusions>
        <exclusion>
          <groupId>xerces</groupId>
          <artifactId>xercesImpl</artifactId>
        </exclusion>
        <exclusion>
          <groupId>xerces</groupId>
          <artifactId>xmlParserAPIs</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    
    0 讨论(0)
  • 2020-12-05 01:13

    Xerces impl is the main culprit here. Remove it. Jdk has inbuilt jaxb parser, you don't need this.

    so, if that dependency is coming from a parent project in case of maven use a exclusion tab in case you can't directly remove it.

    <exclusion>
                     <groupId>xerces</groupId>  
                <artifactId>xercesImpl</artifactId> 
                    </exclusion>
    

    The reason this problem is so hard to detect is because, when you usually write a jaxb unmarshalling code

    you will do a unmarshalling on a try block and then catch jaxb exception and then do whatever with the error.

    But this culprit parser of a jar (xercesimpl) throws a runtime exception in the middle causing error to not get logged and will be only be detected after careful debugging. Look at the code snippet below

    try {
    JAXBContext context = JAXBContext.newInstance(YourClass.class);
                Unmarshaller unmarshaller = context.createUnmarshaller();
                YourClass object = (YourClass)unmarshaller.unmarshal(new StringReader("SomeXmlInString"));
    
    
    }
    
    catch (JAXBException e){
    e.printStackTrace();
    
    }
    

    Here xercesImpl causes the unmarshaller to use some other sax parser (instead of the regular jaxb parser) causing it to throw different exception which won't be caught in our catch block which is expecting a jaxbexception or one of its subclasses.

    0 讨论(0)
  • 2020-12-05 01:14

    It was a dependency problem.

    Here is my way how I solved the problem:

    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)

    3. 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();
            }
        }
    }
    

    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>
    

    Hope that helps.

    0 讨论(0)
  • 2020-12-05 01:19

    I resolved this problem on my project with the second Mitch ‘solution but just with

    java -Djavax.xml.parsers.SAXParserFactory="com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"
    
    0 讨论(0)
提交回复
热议问题