【问题标题】:Unmarshalling in JAXB在 JAXB 中解组
【发布时间】:2011-11-17 12:04:43
【问题描述】:

我有以下 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 映射类如下。
首先是标签

对应的类
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;
  }

}

接下来是标签对应的类

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 解组为对象并使用以下代码片段在我的控制台上打印字段名称和值。

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

有人能告诉我我在这里做错了什么吗?如果有人能告诉我如何打印我的所有 以及他们的姓名和价值观,我将不胜感激。 p>

提前致谢。
阿塞拉。

【问题讨论】:

    标签: jaxb unmarshalling


    【解决方案1】:

    你可以引入一个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;
        }
    
    }
    

    更多信息

    【讨论】:

      【解决方案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
      

      然后我更改了相关 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;
      }
      

      希望这对其他人也有帮助。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多