gpt4 book ai didi

java - JAXB unMarshaller 出错?

转载 作者:行者123 更新时间:2023-11-30 10:53:58 27 4
gpt4 key购买 nike

我在读取 .xml 文件时得到以下信息。这是错误

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.dJohn.com/teacher", local:"teacher"). Expected elements are (none)

这是我的 file.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Teacher xmlns="http://www.dJohn.com/teacher" Id="0001" />

主类

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class convertorDemo {
public static void main(String[] args) {

try {

File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Teacher.class);

Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Teacher mrS= (Teacher ) jaxbUnmarshaller.unmarshal(file);


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

}

这是Teacher.java..留下一些其他细节

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;



@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Teacher", propOrder = {
})
public class Teacher{

@XmlAttribute(name = "Id")
protected String Id;


public String getId() {
return Id;
}


public void setId(String value) {
this.Id = value;
}

}

我不知道我的问题是什么,我是 JAXB 的新手。请帮忙!!

最佳答案

使用 XmlRootElement 注释您的 Tana 类。

关于java - JAXB unMarshaller 出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33806452/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com