这是我的XML文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dragonDatabase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dragon>
<Name>Hackatoo</Name>
<Description>This impulsive and reckless member of the Sharp Class is a master at collecting wood...so long as its axe-like snout doesn't get stuck in the tree its cutting! Hackatoo eggs laid at a high altitude can hook on to whatever they hit.</Description>
<Class>Sharp</Class>
<Fire-Type>No Data</Fire-Type>
<Diet>No Data</Diet>
</dragon>
<dragon>
<Name>Hobblegrunt</Name>
<Description>The Hobblegrunt has a single horn and and an expandable frill surrounding its head. It has clawed wings, small arms and big legs like a Deadly Nadder. It also appears to have long neck and tail as well. The Hobblegrunt doesn't have a particular color, but instead it changes color depending on its mood.</Description>
<Class>Stoker, Boulder</Class>
<Fire-Type>Ethane Expectorant</Fire-Type>
<Diet>No Data</Diet>
</dragon>
</dragonDatabase>
这是我的DragonBean类:
public class DragonBean {
private String name;
private String description;
private String dragonClass;
private String fireType;
private String diet;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDragonClass() {
return dragonClass;
}
public void setDragonClass(String dragonClass) {
this.dragonClass = dragonClass;
}
public String getFireType() {
return fireType;
}
public void setFireType(String fireType) {
this.fireType = fireType;
}
public String getDiet() {
return diet;
}
public void setDiet(String diet) {
this.diet = diet;
}
}
基本上我想在xml中获取每个龙并将其放在包含DragonBean类型的列表中。我知道我必须解析XML。但我不知道从哪里开始。将信息放入列表后,我计划使用Jackson将其转换为JSON文件。
答案 0 :(得分:0)
使用DocumentBuilderFactory和DocumentBuilder类,解析xml文件。首先获取龙的节点列表,然后逐个迭代它。在每次迭代中获取子节点,然后读取获取这些标记的文本内容并设置模型类。将工作 。希望它有所帮助!