解析JAXB类时获取意外的元素异常

时间:2015-02-08 13:30:57

标签: java jaxb

请帮助我解决这个问题:

这里我试图解组XML文件并收到以下错误:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"question"). Expected elements are <{http://data.schemas.financial.abc.com/Common/2009-09-01/}question>
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)

课程是:

Question.java:

package JAXBTest;

import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name="question",namespace="http://data.schemas.financial.abc.com/Common/2009-09-01/")
    public class Question {  
    private int id;  
    private String questionname;  
    private List<Answer> answers;  
    public Question() {}  
    public Question(int id, String questionname, List<Answer> answers) {  
        super();  
        this.id = id;  
        this.questionname = questionname;  
        this.answers = answers;  
    }  
    @XmlAttribute  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    @XmlElement  
    public String getQuestionname() {  
        return questionname;  
    }  
    public void setQuestionname(String questionname) {  
        this.questionname = questionname;  
    }  
    @XmlElement  
    public List<Answer> getAnswers() {  
        return answers;  
    }  
    public void setAnswers(List<Answer> answers) {  
        this.answers = answers;  
    }   

}

Answer.java

package JAXBTest;

public class Answer {  
private int id;  
private String answername;  
private String postedby;  
public Answer() {}  
public Answer(int id, String answername, String postedby) {  
    super();  
    this.id = id;  
    this.answername = answername;  
    this.postedby = postedby;  
}  
public int getId() {  
    return id;  
}  
public void setId(int id) {  
    this.id = id;  
}  
public String getAnswername() {  
    return answername;  
}  
public void setAnswername(String answername) {  
    this.answername = answername;  
}  
public String getPostedby() {  
    return postedby;  
}  
public void setPostedby(String postedby) {  
    this.postedby = postedby;  
}  

}  

XmltoObject.java

package JAXBTest;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

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

public class XmltoObject {

    public static void main(String[] args) throws FileNotFoundException {  

         try {  

            File file = new File("C:\\Users\\u6026472\\Desktop\\question.xml");  
            JAXBContext jaxbContext = JAXBContext.newInstance(JAXBTest.Question.class);  

      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  

      Question que= (Question) jaxbUnmarshaller.unmarshal(file);  

            System.out.println(que.getId()+" "+que.getQuestionname());  
            System.out.println("Answers:");  
            List<Answer> list=que.getAnswers();  
            for(Answer ans:list)  
              System.out.println(ans.getId()+" "+ans.getAnswername()+"  "+ans.getPostedby());  




          } catch (JAXBException e) {  
            e.printStackTrace();  
          }  

        } 
}

package-info.java

@javax.xml.bind.annotation.XmlSchema(namespace = "http://data.schemas.financial.abc.com/Common/2009-09-01/")
package JAXBTest;

XML文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<question id="1" xmlns:cmn="http://data.schemas.financial.abc.com/Common/2009-09-01/">  
        <answers>  
            <answername>java is a programming language</answername>  
            <id>101</id>  
            <postedby>ravi</postedby>  
        </answers>  
        <answers>  
            <answername>java is a platform</answername>  
            <id>102</id>  
            <postedby>john</postedby>  
        </answers>  
        <questionname>What is java?</questionname>  
    </question>  

1 个答案:

答案 0 :(得分:0)

根据您的映射,JAXB期望XML为:

<cmn:question id="1" xmlns:cmn="http://data.schemas.financial.abc.com/Common/2009-09-01/">  
    <answers>...</answers>
</cmn:question>  

而不是您目前解组的内容:

<question id="1" xmlns:cmn="http://data.schemas.financial.abc.com/Common/2009-09-01/">  
    <answers>...</answers>
</question>  

您可以采取以下措施来解决此问题:

  1. 按照我在此答案中指出的那样修复XML。
  2. namespace注释中删除@XmlRootElement属性,以使您的映射与XML匹配。
  3. 如果您无法更改XML或类,那么您可以在处理XML时调整XML:
  4. 您可以让JAXB使用一个带有Class参数的unmarshal方法忽略根元素。