简体   繁体   English

具有相同名称的JAXB解组元素

[英]JAXB unmarshalling elements with same name

this is my xml structure 这是我的xml结构

<catalog>
    <course>
        <course_id></course_id>
        <subjects>
            <subject>
                <subject_id></subject_id>
            </subject>
            <subject>
                <subject_id></subject_id>
            </subject>
        </subjects>
    </course>
</catalog>

So, i'v tried to bind this xml to a class using JAXB Unmarshalling, but the result was nothing. 因此,我尝试使用JAXB Unmarshalling将此xml绑定到类,但结果一无所获。

I was thinking, i have 2 base elements, course and subject, so i built 2 classes based on these elements. 我当时在想,我有2个基本要素,课程和主题,因此我根据这些要素构建了2个类。
This to control the course tag 这可以控制课程标签

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

}

And This to control the subject tag. 并以此来控制主题标签。

public class subject {
    String subject_id;

    @XmlElement(name="subject_id")
    public void setSubjectId(String id) {
        this.subject_id = id;
    }
}

I made some to string functions, and my output was nothing. 我对字符串函数做了一些,但是我的输出却一无所有。 What is the problem? 问题是什么?

The course element also have a wrapper tag <course> So either you need to change you xml to remove <catalog> tag and make <course> as the root. course元素还具有包装器标记<course>因此您要么需要更改xml才能删除<catalog>标记,然后将<course>作为根。 Or you should create a new class catalog and make course as a field. 或者,您应该创建一个新的班级目录并将课程设置为字段。 Like 喜欢

@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 + "]";
    }
}

A complete example 一个完整的例子

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM