How do I parse this XML in Java with JAXB?(如何使用 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>
更多信息
- http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html
更新
有没有简单的方法可以迭代/打印出所有 ChildID家庭班?
Is there I easy way I can iterate/print out all the ChildID in the Family class?
您可以执行以下操作:
for(Family family : fosterHome.getFamilies()) {
System.out.println(family.getParentID());
for(String childID : family.getChildList()) {
System.out.println(" " + childID);
}
}
这篇关于如何使用 JAXB 在 Java 中解析这个 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 JAXB 在 Java 中解析这个 XML?
基础教程推荐
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01