2012-04-05 78 views
2

我正在创建一个Java类来映射具有多个相同元素的XML文件。以下的XML的文件是什么样子的例子:对具有许多相同元素的XML文件使用JAXB

<TrackBroadcast> 
<DateTime>some date</DateTime> 
<From>from someone</From> 
<To>to someone</To> 
<Classification>some type of classification</Classification> 
<Command>some type of command</Command> 
<MsgId>some id</MsgId> 
<Barge attribute1="???" attribute2="???" attribute3="" etc/> --->The Barge.java class explains the attributes 
<Barge attribute1="???" attribute2="???" attribute3="" etc/> --->The Barge.java class explains the attributes 
<Barge attribute1="???" attribute2="???" attribute3="" etc/> --->The Barge.java class explains the attributes 
<Barge attribute1="???" attribute2="???" attribute3="" etc/> --->The Barge.java class explains the attributes 
</TrackBroadcast> 

我JAXB类可以在日期时间读,发件人,收件人,分类,命令和的MsgId元素,但它不能在驳船元素读取。我有两个类来试图封装XML,但我知道我做错了什么。这两个类是:

@XmlRootElement(name="TrackBroadcast") 
public class TrackBroadcast { 

    String dataTime; 
    String from; 
    String to; 
    String classification; 
    String command; 
    String msgId; 
    List<Barge> barge = new ArrayList<Barge>(); 

    public String getDataTime() { 
     return dataTime; 
    } 

    @XmlElement(name="DateTime") 
    public void setDataTime(String dataTime) { 
     this.dataTime = dataTime; 
    } 

    public String getFrom() { 
     return from; 
    } 

    @XmlElement(name="From") 
    public void setFrom(String from) { 
     this.from = from; 
    } 

    public String getTo() { 
     return to; 
    } 

    @XmlElement(name="To") 
    public void setTo(String to) { 
     this.to = to; 
    } 

    public String getClassification() { 
     return classification; 
    } 

    @XmlElement(name="Classification") 
    public void setClassification(String classification) { 
     this.classification = classification; 
    } 

    public String getCommand() { 
     return command; 
    } 

    @XmlElement(name="Command") 
    public void setCommand(String command) { 
     this.command = command; 
    } 

    public String getMsgId() { 
     return msgId; 
    } 

    @XmlElement(name="MsgId") 
    public void setMsgId(String msgId) { 
     this.msgId = msgId; 
    } 

    public List<Barge> getBarge() { 
     return barge; 
    } 

    public void setBarge(List<Barge> barge) { 
     this.barge = barge; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final TrackBroadcast other = (TrackBroadcast) obj; 
     if ((this.dataTime == null) ? (other.dataTime != null) : !this.dataTime.equals(other.dataTime)) { 
      return false; 
     } 
     if ((this.from == null) ? (other.from != null) : !this.from.equals(other.from)) { 
      return false; 
     } 
     if ((this.to == null) ? (other.to != null) : !this.to.equals(other.to)) { 
      return false; 
     } 
     if ((this.command == null) ? (other.command != null) : !this.command.equals(other.command)) { 
      return false; 
     } 
     if ((this.msgId == null) ? (other.msgId != null) : !this.msgId.equals(other.msgId)) { 
      return false; 
     } 
     if (this.barge != other.barge && (this.barge == null || !this.barge.equals(other.barge))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 7; 
     hash = 43 * hash + (this.dataTime != null ? this.dataTime.hashCode() : 0); 
     hash = 43 * hash + (this.from != null ? this.from.hashCode() : 0); 
     hash = 43 * hash + (this.to != null ? this.to.hashCode() : 0); 
     hash = 43 * hash + (this.classification != null ? this.classification.hashCode() : 0); 
     hash = 43 * hash + (this.command != null ? this.command.hashCode() : 0); 
     hash = 43 * hash + (this.msgId != null ? this.msgId.hashCode() : 0); 
     hash = 43 * hash + (this.barge != null ? this.barge.hashCode() : 0); 
     for(int i=0; i<barge.size(); i++){ 
      Barge b = barge.get(i); 
      System.out.println(b.toString()); 
     } 
     return hash; 
    } 

    @Override 
    public String toString() { 
     return "TrackBroadcast{" + "dataTime=" + dataTime + ", from=" + from + ", to=" + to + ", classification=" + classification + ", command=" + command + ", msgId=" + msgId + ", barge=" + barge + '}'; 
    } 
} 

@XmlRootElement(name="Barge") 
public class Barge { 
    String misleBargeVesselId; 
    String bargeName; 
    String towingVesselName; 
    String nonVesselName; 
    String towingVesselPhoneNo; 
    String towingVesselCompany; 
    String positionDate; 
    double latitude; 
    double longitude; 
    String waterwayAbbr; 
    double waterwayMileMarker; 
    String bargeDirection; 
    String bargeCdcType; 
    double bargeCdcQuantity; 
    String bargeCdcMeasureUnit; 
    String bargeLoadStatus; 
    String nextEta; 


    public String getMisleBargeVesselId() { 
     return misleBargeVesselId; 
    } 

    @XmlAttribute(name="MISLE_Barge_Vessel_Id") 
    public void setMisleBargeVesselId(String misleBargeVesselId) { 
     this.misleBargeVesselId = misleBargeVesselId; 
    } 

    public String getBargeName() { 
     return bargeName; 
    } 

    @XmlAttribute(name="Barge_Name") 
    public void setBargeName(String bargeName) { 
     this.bargeName = bargeName; 
    } 

    public String getTowingVesselName() { 
     return towingVesselName; 
    } 

    @XmlAttribute(name="Towing_Vessel_Name") 
    public void setTowingVesselName(String towingVesselName) { 
     this.towingVesselName = towingVesselName; 
    } 

    public String getNonVesselName() { 
     return nonVesselName; 
    } 

    @XmlAttribute(name="Non_Vessel_Name") 
    public void setNonVesselName(String nonVesselName) { 
     this.nonVesselName = nonVesselName; 
    } 

    public String getTowingVesselPhoneNo() { 
     return towingVesselPhoneNo; 
    } 

    @XmlAttribute(name="Towing_Vessel_Phone_No") 
    public void setTowingVesselPhoneNo(String towingVesselPhoneNo) { 
     this.towingVesselPhoneNo = towingVesselPhoneNo; 
    } 

    public String getTowingVesselCompany() { 
     return towingVesselCompany; 
    } 

    @XmlAttribute(name="Towing_Vessel_Company") 
    public void setTowingVesselCompany(String towingVesselCompany) { 
     this.towingVesselCompany = towingVesselCompany; 
    } 

    public String getPositionDate() { 
     return positionDate; 
    } 

    @XmlAttribute(name="Position_Date") 
    public void setPositionDate(String positionDate) { 
     this.positionDate = positionDate; 
    } 

    public double getLatitude() { 
     return latitude; 
    } 

    @XmlAttribute(name="Latitude") 
    public void setLatitude(double latitude) { 
     this.latitude = latitude; 
    } 

    public double getLongitude() { 
     return longitude; 
    } 

    @XmlAttribute(name="Longitude") 
    public void setLongitude(double longitude) { 
     this.longitude = longitude; 
    } 

    public String getWaterwayAbbr() { 
     return waterwayAbbr; 
    } 

    @XmlAttribute(name="Waterway_Abbr") 
    public void setWaterwayAbbr(String waterwayAbbr) { 
     this.waterwayAbbr = waterwayAbbr; 
    } 

    public double getWaterwayMileMarker() { 
     return waterwayMileMarker; 
    } 

    @XmlAttribute(name="Waterway_Mile_Marker") 
    public void setWaterwayMileMarker(double waerwayMileMarker) { 
     this.waterwayMileMarker = waerwayMileMarker; 
    } 

    public String getBargeDirection() { 
     return bargeDirection; 
    } 

    @XmlAttribute(name="Barge_Direction") 
    public void setBargeDirection(String bargeDirection) { 
     this.bargeDirection = bargeDirection; 
    } 

    public String getBargeCdcType() { 
     return bargeCdcType; 
    } 

    @XmlAttribute(name="Barge_CDC_Type") 
    public void setBargeCdcType(String bargeCdcType) { 
     this.bargeCdcType = bargeCdcType; 
    } 

    public double getBargeCdcQuantity() { 
     return bargeCdcQuantity; 
    } 

    @XmlAttribute(name="Barge_CDC_Quantity") 
    public void setBargeCdcQuantity(double bargeCdcQuantity) { 
     this.bargeCdcQuantity = bargeCdcQuantity; 
    } 

    public String getBargeCdcMeasureUnit() { 
     return bargeCdcMeasureUnit; 
    } 

    @XmlAttribute(name="Barge_CDC_Measure_Unit") 
    public void setBargeCdcMeasureUnit(String bargeCdcMeasureUnit) { 
     this.bargeCdcMeasureUnit = bargeCdcMeasureUnit; 
    } 

    public String getBargeLoadStatus() { 
     return bargeLoadStatus; 
    } 

    @XmlAttribute(name="Barge_Load_Status") 
    public void setBargeLoadStatus(String bargeLoadStatus) { 
     this.bargeLoadStatus = bargeLoadStatus; 
    } 

    public String getNextEta() { 
     return nextEta; 
    } 

    @XmlAttribute(name="Next_ETA") 
    public void setNextEta(String nextEta) { 
     this.nextEta = nextEta; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final Barge other = (Barge) obj; 
     if ((this.misleBargeVesselId == null) ? (other.misleBargeVesselId != null) : !this.misleBargeVesselId.equals(other.misleBargeVesselId)) { 
      return false; 
     } 
     if ((this.bargeName == null) ? (other.bargeName != null) : !this.bargeName.equals(other.bargeName)) { 
      return false; 
     } 
     if ((this.towingVesselName == null) ? (other.towingVesselName != null) : !this.towingVesselName.equals(other.towingVesselName)) { 
      return false; 
     } 
     if ((this.nonVesselName == null) ? (other.nonVesselName != null) : !this.nonVesselName.equals(other.nonVesselName)) { 
      return false; 
     } 
     if ((this.towingVesselPhoneNo == null) ? (other.towingVesselPhoneNo != null) : !this.towingVesselPhoneNo.equals(other.towingVesselPhoneNo)) { 
      return false; 
     } 
     if ((this.towingVesselCompany == null) ? (other.towingVesselCompany != null) : !this.towingVesselCompany.equals(other.towingVesselCompany)) { 
      return false; 
     } 
     if ((this.positionDate == null) ? (other.positionDate != null) : !this.positionDate.equals(other.positionDate)) { 
      return false; 
     } 
     if (Double.doubleToLongBits(this.latitude) != Double.doubleToLongBits(other.latitude)) { 
      return false; 
     } 
     if (Double.doubleToLongBits(this.longitude) != Double.doubleToLongBits(other.longitude)) { 
      return false; 
     } 
     if ((this.waterwayAbbr == null) ? (other.waterwayAbbr != null) : !this.waterwayAbbr.equals(other.waterwayAbbr)) { 
      return false; 
     } 
     if (Double.doubleToLongBits(this.waterwayMileMarker) != Double.doubleToLongBits(other.waterwayMileMarker)) { 
      return false; 
     } 
     if ((this.bargeDirection == null) ? (other.bargeDirection != null) : !this.bargeDirection.equals(other.bargeDirection)) { 
      return false; 
     } 
     if ((this.bargeCdcType == null) ? (other.bargeCdcType != null) : !this.bargeCdcType.equals(other.bargeCdcType)) { 
      return false; 
     } 
     if (Double.doubleToLongBits(this.bargeCdcQuantity) != Double.doubleToLongBits(other.bargeCdcQuantity)) { 
      return false; 
     } 
     if ((this.bargeCdcMeasureUnit == null) ? (other.bargeCdcMeasureUnit != null) : !this.bargeCdcMeasureUnit.equals(other.bargeCdcMeasureUnit)) { 
      return false; 
     } 
     if ((this.bargeLoadStatus == null) ? (other.bargeLoadStatus != null) : !this.bargeLoadStatus.equals(other.bargeLoadStatus)) { 
      return false; 
     } 
     if ((this.nextEta == null) ? (other.nextEta != null) : !this.nextEta.equals(other.nextEta)) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 5; 
     hash = 17 * hash + (this.misleBargeVesselId != null ? this.misleBargeVesselId.hashCode() : 0); 
     hash = 17 * hash + (this.bargeName != null ? this.bargeName.hashCode() : 0); 
     hash = 17 * hash + (this.towingVesselName != null ? this.towingVesselName.hashCode() : 0); 
     hash = 17 * hash + (this.nonVesselName != null ? this.nonVesselName.hashCode() : 0); 
     hash = 17 * hash + (this.towingVesselPhoneNo != null ? this.towingVesselPhoneNo.hashCode() : 0); 
     hash = 17 * hash + (this.towingVesselCompany != null ? this.towingVesselCompany.hashCode() : 0); 
     hash = 17 * hash + (this.positionDate != null ? this.positionDate.hashCode() : 0); 
     hash = 17 * hash + (int) (Double.doubleToLongBits(this.latitude)^(Double.doubleToLongBits(this.latitude) >>> 32)); 
     hash = 17 * hash + (int) (Double.doubleToLongBits(this.longitude)^(Double.doubleToLongBits(this.longitude) >>> 32)); 
     hash = 17 * hash + (this.waterwayAbbr != null ? this.waterwayAbbr.hashCode() : 0); 
     hash = 17 * hash + (int) (Double.doubleToLongBits(this.waterwayMileMarker)^(Double.doubleToLongBits(this.waterwayMileMarker) >>> 32)); 
     hash = 17 * hash + (this.bargeDirection != null ? this.bargeDirection.hashCode() : 0); 
     hash = 17 * hash + (this.bargeCdcType != null ? this.bargeCdcType.hashCode() : 0); 
     hash = 17 * hash + (int) (Double.doubleToLongBits(this.bargeCdcQuantity)^(Double.doubleToLongBits(this.bargeCdcQuantity) >>> 32)); 
     hash = 17 * hash + (this.bargeCdcMeasureUnit != null ? this.bargeCdcMeasureUnit.hashCode() : 0); 
     hash = 17 * hash + (this.bargeLoadStatus != null ? this.bargeLoadStatus.hashCode() : 0); 
     hash = 17 * hash + (this.nextEta != null ? this.nextEta.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public String toString() { 
     return "Barge{" + "misleBargeVesselId=" + misleBargeVesselId + ", bargeName=" + bargeName + ", towingVesselName=" + towingVesselName + ", nonVesselName=" + nonVesselName + ", towingVesselPhoneNo=" + towingVesselPhoneNo + ", towingVesselCompany=" + towingVesselCompany + ", positionDate=" + positionDate + ", latitude=" + latitude + ", longitude=" + longitude + ", waterwayAbbr=" + waterwayAbbr + ", waerwayMileMarker=" + waterwayMileMarker + ", bargeDirection=" + bargeDirection + ", bargeCdcType=" + bargeCdcType + ", bargeCdcQuantity=" + bargeCdcQuantity + ", bargeCdcMeasureUnit=" + bargeCdcMeasureUnit + ", bargeLoadStatus=" + bargeLoadStatus + ", nextEta=" + nextEta + '}'; 
    } 
} 

当我解组文件,我可以在TrackBroadcast得到的一切除了驳船。我是JAXB的新手,想知道是否有人能够看到我可能做错了什么,或者是否有人能够让我朝正确的方向发展。

回答

0

只需将JAXB @XmlElement注释与首选名称相比,在必填字段设置器上定义即可。此外,我认为从XML Schema生成类比手动确保映射合约更好。

相关问题