本文介绍了如何使用 JAXB 在 Java 中解析这个 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML,没有 XSD 或模式,我想使用 JAXB 解析为 java 对象,因为我听说它比 SAX 更好.有没有办法通过注释或更好的方法来实现这一点?它是否使我只需要一个 FosterHome 课程?我无法找到如何做到这一点,任何帮助都非常感谢.

I have the following XML, no XSD or schema with it that I want to parse to java object(s) using JAXB as I heard its better than SAX. Is there a way to accomplish this with annotations or a better way to do this? Does it make it so that i just need a single FosterHome class? I am having trouble finding how to do this any help is grateful.

我最初想开设 FosterHome、Family 和 Child 课程.使用 JAXB,是否不再需要 3 个类?由于 ChildID 出现在两个不同的区域,我不确定如何处理.

I was originally thinking of having a FosterHome, Family, and Child class. Using JAXB, is having 3 classes no longer necessary? Im not sure how to deal with this as ChildID shows up in two different areas.

<?xml version="1.0" encoding="UTF-8"?>
<FosterHome>
    <Orphanage>Happy Days Daycare</Orphanage>
    <Location>Apple Street</Location>
    <Families>
        <Family>
            <ParentID>Adams</ParentID>
            <ChildList>
                <ChildID>Child1</ChildID>
                <ChildID>Child2</ChildID>
            </ChildList>
        </Family>
        <Family>
            <ParentID>Adams</ParentID>
            <ChildList>
                <ChildID>Child3</ChildID>
                <ChildID>Child4</ChildID>
            </ChildList>
        </Family>
    </Families>
    <RemainingChildList>
        <ChildID>Child5</ChildID>
        <ChildID>Child6</ChildID>
    </RemainingChildList>
</FosterHome>

推荐答案

您可以执行以下操作.通过利用 @XmlElementWrapper,您可以减少所需的类数量:

You could do the following. By leveraging @XmlElementWrapper you can reduce the amount of classes that you require:

福斯特之家

package nov18;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="FosterHome")
@XmlAccessorType(XmlAccessType.FIELD)
public class FosterHome {

    @XmlElement(name="Orphanage")
    private String orphanage;

    @XmlElement(name="Location")
    private String location;

    @XmlElementWrapper(name="Families")
    @XmlElement(name="Family")
    private List<Family> families;

    @XmlElementWrapper(name="RemainingChildList")
    @XmlElement(name="ChildID")
    private List<String> remainingChildren;

}

家庭

package nov18;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Family {

    @XmlElement(name="ParentID")
    private String parentID;

    @XmlElementWrapper(name="ChildList")
    @XmlElement(name="ChildID")
    private List<String> childList;

}

演示

package nov18;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(FosterHome.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        FosterHome fosterHome = (FosterHome) unmarshaller.unmarshal(new File("src/nov18/input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(fosterHome, System.out);
    }

}

输入/输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FosterHome>
    <Orphanage>Happy Days Daycare</Orphanage>
    <Location>Apple Street</Location>
    <Families>
        <Family>
            <ParentID>Adams</ParentID>
            <ChildList>
                <ChildID>Child1</ChildID>
                <ChildID>Child2</ChildID>
            </ChildList>
        </Family>
        <Family>
            <ParentID>Adams</ParentID>
            <ChildList>
                <ChildID>Child3</ChildID>
                <ChildID>Child4</ChildID>
            </ChildList>
        </Family>
    </Families>
    <RemainingChildList>
        <ChildID>Child5</ChildID>
        <ChildID>Child6</ChildID>
    </RemainingChildList>
</FosterHome>

更多信息

更新

有没有简单的方法可以迭代/打印出所有 ChildID家庭班?

您可以执行以下操作:

    for(Family family : fosterHome.getFamilies()) {
        System.out.println(family.getParentID());
        for(String childID : family.getChildList()) {
            System.out.println("    " + childID);
        }
    }

这篇关于如何使用 JAXB 在 Java 中解析这个 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:12