javax.xml.bind.UnmarshalException: 意外元素 (uri:"", local:"TestSubject")。预期元素为 <{}Test>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21821272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:52:30  来源:igfitidea点击:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"TestSubject"). Expected elements are <{}Test>

javaxmljaxb

提问by

There are several help topics concerning the issue but I haven't found a solution that solved my issue. I appreciate guidance in solving the issue.

有几个关于该问题的帮助主题,但我还没有找到解决我的问题的解决方案。我很欣赏解决问题的指导。

I tried generating xml thru Marshal and below xml is what is generated from code.

我尝试通过 Marshal 生成 xml,下面的 xml 是从代码生成的。

I have to work with xml structure below:

我必须使用下面的 xml 结构:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Test>
    <testSubject>
        <firstName>test1</firstName>`enter code here`
        <lastNAme>lastname</lastNAme>
        <ssn>123456</ssn>
    </testSubject>
</Test>

code

代码

@XmlRootElement(name = "Test")
public class Test { 
      public Test()
      {
          testSubject = new ArrayList<TestSubject>();
      }

    List<TestSubject> testSubject;

    @XmlElement(name = "testSubject", type = TestSubject.class)         
    public List<TestSubject> getTestSubject() {
        return testSubject;
    }

    public void setTestSubject(List<TestSubject> testSubject) {
        this.testSubject = testSubject;
    }

TestSubject class

测试主题类

public class TestSubject {

    String firstName;
    String lastNAme;
    int ssn;

//getters and setters

}

//unmarshall code in my main class

//在我的主类中解组代码

JAXBContext jc = JAXBContext.newInstance(Test.class);

Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("c://testSubjects.xml");
Test tests = (Test) unmarshaller.unmarshal(xml);

Exception

例外

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"TestSubject"). Expected elements are <{}Test>
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)

回答by Sotirios Delimanolis

It seems like you aren't showing us the real XML you are trying to unmarshal. The error you are seeing would happen if your XML was in the form

您似乎没有向我们展示您试图解组的真正 XML。如果您的 XML 是在表单中,则会发生您看到的错误

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

<testSubject>
    <firstName>test1</firstName>
    <lastNAme>lastname</lastNAme>
    <ssn>123456</ssn>
</testSubject>

instead of

代替

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Test>
    <testSubject>
        <firstName>test1</firstName>
        <lastNAme>lastname</lastNAme>
        <ssn>123456</ssn>
    </testSubject>
</Test>

Simply correct it.

简单地纠正一下。

As the stacktrace says

正如堆栈跟踪所说

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"TestSubject"). Expected elements are <{}Test>

Your are getting a <testSubject>node where you are expecting a <Test>node. Since <Test>is meant to be the root node, that's where it occurs.

您正在获得一个<testSubject>您期望<Test>节点的节点。既然<Test>是根节点,那就是它发生的地方。