gpt4 book ai didi

java - JAXB 解码具有相同名称的元素

转载 作者:行者123 更新时间:2023-12-01 11:09:00 25 4
gpt4 key购买 nike

这是我的 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 绑定(bind)到一个类,但结果一无所获。

我在想,我有 2 个基本元素,类(class)和主题,所以我基于这些元素构建了 2 个类(class)。
这可以控制类(class)标签

@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;
}
}

我做了一些字符串函数,但我的输出什么也没有。有什么问题吗?

最佳答案

类(class)元素还有一个包装标签 <course>因此,您需要更改 xml 以删除 <catalog>标记并制作 <course>作为根。或者您应该创建一个新的类(class)目录并将类(class)作为一个字段。喜欢

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

}
}

关于java - JAXB 解码具有相同名称的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32590234/

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