我有以下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>
Run Code Online (Sandbox Code Playgroud)
bdo*_*han 60
您可以执行以下操作.通过利用,@XmlElementWrapper您可以减少所需的类数量:
FosterHome
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;
}
Run Code Online (Sandbox Code Playgroud)
家庭
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;
}
Run Code Online (Sandbox Code Playgroud)
演示
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);
}
}
Run Code Online (Sandbox Code Playgroud)
输入输出
<?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>
Run Code Online (Sandbox Code Playgroud)
欲获得更多信息
UPDATE
我可以通过简单的方法迭代/打印Family类中的所有ChildID吗?
您可以执行以下操作:
for(Family family : fosterHome.getFamilies()) {
System.out.println(family.getParentID());
for(String childID : family.getChildList()) {
System.out.println(" " + childID);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57512 次 |
| 最近记录: |