在JAXB中解组

时间:2011-11-17 12:04:44

标签: 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>
    <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映射类如下所示 首先是对应于标签&lt;的类。结果集&gt;

package com.wapice.xml.beans;

import java.util.ArrayList;

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


@XmlRootElement(name = "resultset")
 public class Resultset {

  private ArrayList<Row> rowsList;


  @XmlElement(required = true, name = "row")
  public ArrayList<Row> getRowsList() {
    return rowsList;
  }


  public void setRowsList(ArrayList<Row> rowsList) {
    this.rowsList = rowsList;
  }

}

接下来是对应于标签&lt;的类。行&gt;

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 String field;


    @XmlElement(required = true, name = "field")
    public String getField() {
      return field;
    }


    public void setField(String field) {
      this.field = field;
    }

}

我试图将这个xml解组为对象&amp;打印字段名称&amp;我的控制台上的值包含以下代码片段。

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

有人可以告诉我这里我做错了吗?如果有人能告诉我如何打印我的所有&lt;字段&gt; 以及他们的姓名&amp;值。

提前致谢。
Asela。

2 个答案:

答案 0 :(得分:2)

您可以引入Field对象:

package com.wapice.xml.beans;

import javax.xml.bind.annotation.*;

public class Field {
    @XmlAttribute name;
    @XmlValue value;
}

Row对象保持在它们的列表中:

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;
    }

}

了解更多信息

答案 1 :(得分:2)

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

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

然后我更改了相关的getters / setters&amp;的名字。它工作得很好。 以下是我改变它的方式。

----------------
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;
}

希望这也有助于其他人。