如何使用JAXB解析Java中的XML?

我有以下XML,没有XSD或架构,我想使用JAXB解析为java对象,因为我听说它比SAX更好。有没有一种方法可以通过注释或更好的方法来完成此任务?它是否使我只需要一个FosterHome类?我很难找到如何执行此操作的任何帮助,不胜感激。

我本来是想开设FosterHome,Family和Child班的。使用JAXB,不再需要3个类吗?我不确定如何处理此问题,因为ChildID在两个不同的区域显示。

<?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您可以减少所需的类数量:

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>

  • http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html


有没有简单的方法可以迭代/打印出Family类中的所有ChildID?

您可以执行以下操作:

    for(Family family : fosterHome.getFamilies()) {

System.out.println(family.getParentID());

for(String childID : family.getChildList()) {

System.out.println(" " + childID);

}

}

以上是 如何使用JAXB解析Java中的XML? 的全部内容, 来源链接: utcz.com/qa/409647.html

回到顶部