Java 如何使用Jaxb而不是从根元素处理xml文件解析
有人能就下一种情况给我一个建议吗: 我的xml文件中有这样的结构:Java 如何使用Jaxb而不是从根元素处理xml文件解析,java,xml,parsing,jaxb,Java,Xml,Parsing,Jaxb,有人能就下一种情况给我一个建议吗: 我的xml文件中有这样的结构: <?xml ... ?> <root> <listof_aaa> <aaa>aaa_object</aaa> <aaa>aaa_object</aaa> ... </listof_aaa> <listof_bbb> <bbb
<?xml ... ?>
<root>
<listof_aaa>
<aaa>aaa_object</aaa>
<aaa>aaa_object</aaa>
...
</listof_aaa>
<listof_bbb>
<bbb>bbb_object</bbb>
<bbb>bbb_object</bbb>
...
</listof_bbb>
<listof_ccc>
<ccc>ccc_object</ccc>
<ccc>ccc_object</ccc>
...
</listof_ccc>
</root>
aaa_对象
aaa_对象
...
bbb_对象
bbb_对象
...
ccc_对象
ccc_对象
...
我的目标是先读取所有aaa对象,然后读取bbb等等。。。
如果在根标记中有几个不同的对象族,如何解析这样的结构?我曾考虑过使用JAXB,但无法理解,在这种情况下使用它有多好
另外,所有对象族都是POJO。
谢谢大家! 在您的案例中,典型的自动生成类如下所示:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"listof_aaa",
"listof_bbb",
"listof_ccc",
"listinside"
})
@XmlRootElement(name = "root")
public class Root {
@XmlElement(required = true)
protected Listof_aaa listof_aaa;
protected Listof_bbb listof_bbb;
protected Listof_ccc listof_ccc;
protected java.util.List<Listinside> listinside // example of multiple elements with one node name under the root
public Listof_aaa getListof_aaa() {
return listof_aaa;
}
public void setListof_aaa(Listof_aaa value) {
this.listof_aaa = value;
}
// other getters and setters
// and an example of multiple elements under the root
public java.util.List<Listinside> getListinside() {
if (listinside == null) {
listinside = new ArrayList<Listinside>();
}
return this.sec;
}
}
@xmlacessortype(xmlacesstype.FIELD)
@XmlType(name=),比例={
“_aaa列表”,
“bbb清单”,
"中华公所名册",,
“列表内部”
})
@XmlRootElement(name=“root”)
公共类根{
@XmlElement(必需=true)
_aaa的受保护列表_aaa的列表;
_bbb的受保护列表_bbb的列表;
_ccc的受保护列表_ccc的列表;
受保护的java.util.List listinside//根目录下具有一个节点名的多个元素的示例
_aaa的公共列表getListof_aaa(){
_aaa的返回列表;
}
公共无效设置_aaa列表(_aaa值列表){
this.listof_aaa=值;
}
//其他的接球手和二传手
//以及根下多个元素的示例
public java.util.List getListinside(){
如果(listinside==null){
listinside=新的ArrayList();
}
返回此值。秒;
}
}
迭代:
public static void main(String[] args) throws JAXBException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarhsaller = jaxbContext.createUnmarshaller();
Root root = (Root) unmarhsaller.unmarshal(new File("pathToYourFile.xml"));
Listof_aaa listof_aa = root.getListof_aaa();
/* and in case of a list */
List<Listinside> listinside = root.getListinside();
for (List myList : listinside) {
// do your stuff
}
}
publicstaticvoidmain(String[]args)抛出jaxbeexception、NoSuchFieldException、SecurityException、IllegalArgumentException、IllegalAccessException{
JAXBContext JAXBContext=JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarhsaller=jaxbContext.createUnmarshaller();
Root=(Root)unmarhsaller.unmarshal(新文件(“pathtoyurfile.xml”);
Listof_aaa Listof_aa=root.getListof_aaa();
/*如果有一份清单*/
List listinside=root.getListinside();
对于(列表myList:listinside){
//做你的事
}
}
在您的案例中,典型的自动生成类如下所示:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"listof_aaa",
"listof_bbb",
"listof_ccc",
"listinside"
})
@XmlRootElement(name = "root")
public class Root {
@XmlElement(required = true)
protected Listof_aaa listof_aaa;
protected Listof_bbb listof_bbb;
protected Listof_ccc listof_ccc;
protected java.util.List<Listinside> listinside // example of multiple elements with one node name under the root
public Listof_aaa getListof_aaa() {
return listof_aaa;
}
public void setListof_aaa(Listof_aaa value) {
this.listof_aaa = value;
}
// other getters and setters
// and an example of multiple elements under the root
public java.util.List<Listinside> getListinside() {
if (listinside == null) {
listinside = new ArrayList<Listinside>();
}
return this.sec;
}
}
@xmlacessortype(xmlacesstype.FIELD)
@XmlType(name=),比例={
“_aaa列表”,
“bbb清单”,
"中华公所名册",,
“列表内部”
})
@XmlRootElement(name=“root”)
公共类根{
@XmlElement(必需=true)
_aaa的受保护列表_aaa的列表;
_bbb的受保护列表_bbb的列表;
_ccc的受保护列表_ccc的列表;
受保护的java.util.List listinside//根目录下具有一个节点名的多个元素的示例
_aaa的公共列表getListof_aaa(){
_aaa的返回列表;
}
公共无效设置_aaa列表(_aaa值列表){
this.listof_aaa=值;
}
//其他的接球手和二传手
//以及根下多个元素的示例
public java.util.List getListinside(){
如果(listinside==null){
listinside=新的ArrayList();
}
返回此值。秒;
}
}
迭代:
public static void main(String[] args) throws JAXBException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarhsaller = jaxbContext.createUnmarshaller();
Root root = (Root) unmarhsaller.unmarshal(new File("pathToYourFile.xml"));
Listof_aaa listof_aa = root.getListof_aaa();
/* and in case of a list */
List<Listinside> listinside = root.getListinside();
for (List myList : listinside) {
// do your stuff
}
}
publicstaticvoidmain(String[]args)抛出jaxbeexception、NoSuchFieldException、SecurityException、IllegalArgumentException、IllegalAccessException{
JAXBContext JAXBContext=JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller unmarhsaller=jaxbContext.createUnmarshaller();
Root=(Root)unmarhsaller.unmarshal(新文件(“pathtoyurfile.xml”);
Listof_aaa Listof_aa=root.getListof_aaa();
/*如果有一份清单*/
List listinside=root.getListinside();
对于(列表myList:listinside){
//做你的事
}
}
您有XML模式吗?如果是,您的自动生成的根类是什么样子的?您有XML模式吗?如果是,您的自动生成的根类是什么样子的?