这是我的xml结构
<catalog>
<course>
<course_id></course_id>
<subjects>
<subject>
<subject_id></subject_id>
</subject>
<subject>
<subject_id></subject_id>
</subject>
</subjects>
</course>
</catalog>
所以,我试图使用JAXB Unmarshalling将这个xml绑定到一个类,但结果却没有。
我在想,我有2个基本元素,课程和主题,所以我根据这些元素构建了2个类 这是为了控制课程标签
@XmlRootElement(name="catalog")
@XmlAccessorType(XmlAccessType.FIELD)
public class curso {
@XmlElement(name="course_id")
int course_id;
@XmlElementWrapper(name="subjects")
@XmlElement(name="subject")
List <subject> subjects = new ArrayList<>();
public void setCourse_id(int curso_id) {
this.curso_id = curso_id;
}
public void setSubjects(List<subject> subjects) {
this.subjects = subjects;
}
}
这是为了控制主题标签。
public class subject {
String subject_id;
@XmlElement(name="subject_id")
public void setSubjectId(String id) {
this.subject_id = id;
}
}
我做了一些字符串函数,我的输出什么都没有。 有什么问题?
答案 0 :(得分:1)
course元素还有一个包装标记<course>
因此,您需要更改xml以删除<catalog>
标记并将<course>
作为根。或者您应该创建一个新的类目录并将课程作为一个字段。像
@XmlRootElement(name="catalog")
@XmlAccessorType(XmlAccessType.FIELD)
class catalog {
@XmlElement(name="course")
curso course;
public curso getCourse() {
return course;
}
public void setCourse(curso course) {
this.course = course;
}
@Override
public String toString() {
return "catalog [course=" + course + "]";
}
}
一个完整的例子
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="catalog")
@XmlAccessorType(XmlAccessType.FIELD)
class catalog {
@XmlElement(name="course")
curso course;
public curso getCourse() {
return course;
}
public void setCourse(curso course) {
this.course = course;
}
@Override
public String toString() {
return "catalog [course=" + course + "]";
}
}
class curso {
@XmlElement(name = "course_id")
int course_id;
@XmlElementWrapper(name = "subjects")
@XmlElement(name = "subject")
List<subject> subjects = new ArrayList<>();
public void setCourse_id(int curso_id) {
this.course_id = curso_id;
}
public void setSubjects(List<subject> subjects) {
this.subjects = subjects;
}
@Override
public String toString() {
return "curso [course_id=" + course_id + ", subjects=" + subjects + "]";
}
}
class subject {
String subject_id;
@XmlElement(name = "subject_id")
public void setSubjectId(String id) {
this.subject_id = id;
}
@Override
public String toString() {
return "subject [subject_id=" + subject_id + "]";
}
}
public class JaxbExample2 {
public static void main(String[] args) {
try {
File file = new File("file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(catalog.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
catalog customer = (catalog) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}