我需要使用SImple XML解析一个大的xml文件,(我真的想使用Simple XML)。我使用XSD创建了对象,将它们从特定于JAXB的对象转换为特定于SimpleXML的对象。
XML看起来像这样:
<House>
<MainLevel Name="~#editRoom" IsHidden="false">
<ChildLevel Name="Television" Category="Livingroom">
<string>TestRoom</string>
</ChildLevel>
<ChildLevel Name="Chair" Category="Livingroom">
<string>TestRoom</string>
</ChildLevel>
<ChildLevel Name="Table">
<string>TestRoom</string>
</ChildLevel>
<ChildLevel Name="ChamberName" Category="Livingroom">
<string>TestRoom</string>
</ChildLevel>
<ChildLevel Name="ChamberName" Category="Bathroom">
<string>BathTub</string>
</ChildLevel>
<ChildLevel Name="Door", Category="DiningRoom">
<boolean>isOpen</boolean>
</ChildLevel>
</MainLevel>
<MainLevel Name="~#editRoom" IsHidden="false">
<ChildLevel Name="Television" Category="Livingroom">
<string>TestRoom</string>
</ChildLevel>
<ChildLevel Name="Chair" Category="Livingroom">
<string>TestRoom</string>
</ChildLevel>
<ChildLevel Name="Table" Category="Livingroom">
<string>TestRoom</string>
</ChildLevel>
<ChildLevel Name="ChamberName" Category="Livingroom">
<string>TestRoom</string>
</ChildLevel>
<ChildLevel Name="ChamberName" Category="Bathroom">
<string>BathTub</string>
</ChildLevel>
<ChildLevel Name="Door">
<boolean>isOpen</boolean>
</ChildLevel>
</MainLevel>
</House>
你有什么建议。请帮忙.Thx。
答案 0 :(得分:3)
你最好写3个课程:
House
类,(=根)包含MainLevel
MainLevel
类,包含所有ChildLevel
ChildLevel
类,包含值以下是 伪代码 :
@Root(...)
public class House
{
@ElementList(inline = true, ...)
private List<MainLevel> levels;
// ...
}
public class MainLevel
{
@Attribute(name = "Name")
private String name;
@Attribute(name = "IsHidden")
private bool hidden;
@ElementList(inline = true, ...)
private List<ChildLevel> childLevels;
// ...
}
public class ChildLevel
{
@Attribute(name = "Name")
private String name;
@Attribute(name = "Category", required = false)
private String category;
// ...
}
由于ChildLevel
可以有不同的类型,因此您必须注意这一点。要么实现所有类型并将它们标记为不需要,要么创建子类。