JAXB中的解组

JAXB中的解组,jaxb,unmarshalling,Jaxb,Unmarshalling,我有以下tester.xml文件,其中包含一些有关事件的信息 <?xml version="1.0"?> <resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <row> <field name="esid">539661</field> <field name="esname">Title 01</field>

我有以下tester.xml文件,其中包含一些有关事件的信息

<?xml version="1.0"?>

<resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
    <field name="esid">539661</field>
    <field name="esname">Title 01</field>
    <field name="eslink">http://www.some_event_link.com</field>
    <field name="estext">Event description 01</field>
    <field name="esinfo" xsi:nil="true" />
    <field name="espicture_small">http://www.some_event_link.com/media/small_image.jpg</field>
    <field name="espicture">http://www.some_event_link.com/media/some_image..gif</field>
    <field name="espicture_big">http://www.some_event_link.com/media/big_image.jpg</field>
    <field name="esbegin">2000-11-22</field>
    <field name="esend">2011-12-15</field>
    <field name="eventid">1379305</field>
    <field name="eventname">Event name 01</field>
    <field name="eventdate">2011-10-12</field>
    <field name="eventtime">19:00:00</field>
    <field name="eventlink">http://www.mysite.com/tickets.html</field>
    <field name="eventvenue">Event venue 01</field>
</row>
<row>
    <field name="esid">539636</field>
    <field name="esname">Title 02</field>
    <field name="eslink">http://www.some_event_link.com</field>
    <field name="estext">Event description 02</field>
    <field name="esinfo" xsi:nil="true" />
    <field name="espicture_small">http://www.some_event_link.com/media/small_image.jpg</field>
    <field name="espicture">http://www.some_event_link.com/media/some_image..gif</field>
    <field name="espicture_big">http://www.some_event_link.com/media/big_image.jpg</field>
    <field name="esbegin">2000-10-10</field>
    <field name="esend">2011-11-01</field>
    <field name="eventid">1379081</field>
    <field name="eventname">Event name 01</field>
    <field name="eventdate">2011-10-12</field>
    <field name="eventtime">14:00:00</field>
    <field name="eventlink">http://www.mysite.com/tickets.html</field>
    <field name="eventvenue">Event venue 02</field>
</row>
我尝试将这个xml解组为对象,并使用下面的代码片段在控制台上打印字段名和值

try {
    JAXBContext context = JAXBContext.newInstance(Resultset.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    Resultset resultSet = (Resultset)unmarshaller.unmarshal(new FileReader("tester.xml"));

    for(Row row : resultSet.getRowsList()){
    System.out.println("Field : " +row.getField());
    }
} 
catch (JAXBException e) {
    e.printStackTrace();
} 
catch (IOException e) {
    e.printStackTrace();
}
但当我运行上述代码时,它只打印最后一个字段的值。输出如下

Field : Event venue 01
Field : Event venue 02
有人能告诉我我做错了什么吗?如果有人能告诉我如何打印我所有的以及它们的名称和值,我将不胜感激

提前感谢。

Asela.

您可以引入
字段
对象:

package com.wapice.xml.beans;

import javax.xml.bind.annotation.*;

public class Field {
    @XmlAttribute name;
    @XmlValue value;
}
并让
对象保留在它们的列表上:

package com.wapice.xml.beans;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


@XmlRootElement(name = "row")
@XmlType(propOrder = {"field"})
public class Row {

    private List<Field> fields;

    @XmlElement(required = true, name = "field")
    public List<Field> getFields() {
      return field;
    }

    public void setField(List<Field> fields) {
      this.fields = fields;
    }

}
package com.wapice.xml.beans;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlRootElement;
导入javax.xml.bind.annotation.XmlType;
@XmlRootElement(name=“row”)
@XmlType(propOrder={“字段”})
公共类行{
私有列表字段;
@XmlElement(必需=true,name=“field”)
公共列表getFields(){
返回字段;
}
公共无效设置字段(列表字段){
this.fields=字段;
}
}
了解更多信息


我用你的帖子解决了我的问题&这真的很有帮助。非常感谢你这么做。然而,我必须做一些修改才能让它工作,因为我的映射类抛出了以下异常

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
Class has two properties of the same name "rowsList"
this problem is related to the following location:
    at public java.util.ArrayList com.wapice.xml.beans.Resultset.getRowsList()
    at com.wapice.xml.beans.Resultset
this problem is related to the following location:
    at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
    at com.wapice.xml.beans.Resultset
Class has two properties of the same name "fieldsList"
this problem is related to the following location:
    at public java.util.ArrayList com.wapice.xml.beans.Row.getFieldsList()
    at com.wapice.xml.beans.Row
    at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
    at com.wapice.xml.beans.Resultset
this problem is related to the following location:
    at private java.util.ArrayList com.wapice.xml.beans.Row.fieldsList
    at com.wapice.xml.beans.Row
    at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
    at com.wapice.xml.beans.Resultset
然后我更改了相关getter/setter的名称&效果很好。 下面是我如何改变它的

----------------
Class Resultset
----------------

@XmlElement(required = true, name = "row")
private ArrayList<Row> rowsList; // I kept the same name for this attribute


public ArrayList<Row> getRowsList() { // I changed this to getRows()
return rowsList;
}


public void setRowsList(ArrayList<Row> rowsList) { // I changed this to setRows()
this.rowsList = rowsList;
}


----------
Class Row
----------

@XmlElement(required = true, name = "field")
private ArrayList<Field> fieldsList;  // I kept the same name for this attribute


public void setFieldsList(ArrayList<Field> fieldsList) { // I changed this to getFields()
this.fieldsList = fieldsList;
}

public ArrayList<Field> getFieldsList() { // I changed this to setFields()
return fieldsList;
}
----------------
类结果集
----------------
@XmlElement(必需=true,name=“行”)
私有数组列表行列表;//我为该属性保留了相同的名称
public ArrayList getRowsList(){//我将其更改为getRows()
返回行列表;
}
public void setRowsList(ArrayList rowsList){//我将其更改为setRows()
this.rowsList=rowsList;
}
----------
班级排
----------
@XmlElement(必需=true,name=“field”)
私有ArrayList字段列表;//我为该属性保留了相同的名称
public void setFieldsList(ArrayList fieldsList){//我将其更改为getFields()
this.fieldsList=fieldsList;
}
public ArrayList getFieldsList(){//我将其更改为setFields()
返回字段列表;
}

希望这对其他人也有帮助

发生异常的原因是默认访问类型为PUBLIC_MEMBER,并且注释位于字段上,JAXB认为您有一个字段和一个属性映射到同一个XML元素。下面的文章应该可以帮助您:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
Class has two properties of the same name "rowsList"
this problem is related to the following location:
    at public java.util.ArrayList com.wapice.xml.beans.Resultset.getRowsList()
    at com.wapice.xml.beans.Resultset
this problem is related to the following location:
    at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
    at com.wapice.xml.beans.Resultset
Class has two properties of the same name "fieldsList"
this problem is related to the following location:
    at public java.util.ArrayList com.wapice.xml.beans.Row.getFieldsList()
    at com.wapice.xml.beans.Row
    at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
    at com.wapice.xml.beans.Resultset
this problem is related to the following location:
    at private java.util.ArrayList com.wapice.xml.beans.Row.fieldsList
    at com.wapice.xml.beans.Row
    at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
    at com.wapice.xml.beans.Resultset
----------------
Class Resultset
----------------

@XmlElement(required = true, name = "row")
private ArrayList<Row> rowsList; // I kept the same name for this attribute


public ArrayList<Row> getRowsList() { // I changed this to getRows()
return rowsList;
}


public void setRowsList(ArrayList<Row> rowsList) { // I changed this to setRows()
this.rowsList = rowsList;
}


----------
Class Row
----------

@XmlElement(required = true, name = "field")
private ArrayList<Field> fieldsList;  // I kept the same name for this attribute


public void setFieldsList(ArrayList<Field> fieldsList) { // I changed this to getFields()
this.fieldsList = fieldsList;
}

public ArrayList<Field> getFieldsList() { // I changed this to setFields()
return fieldsList;
}