2012-04-05 9 views
2

Ich arbeite an dem Erstellen einer Java-Klasse, um eine XML-Datei zuzuordnen, die mehrere gleiche Elemente aufweist. Die folgende XML ist ein Beispiel dafür, was die Datei wie folgt aussieht:Verwendung von JAXB mit einer XML-Datei mit vielen der gleichen Elemente

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

Meine JAXB-Klasse in der Datum- und Von, Klassifizierung, Befehl und MsgId Elementen lesen kann, aber es kann in den Barge Elementen nicht lesen. Ich habe zwei Klassen, um das XML zu kapseln, aber ich weiß, dass ich etwas falsch mache. Die beiden Klassen sind:

@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 + '}'; 
    } 
} 

und

@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 + '}'; 
    } 
} 

Wenn ich die Datei entordnen ich alles in der TrackBroadcast mit Ausnahme der Lastkähne bekommen. Ich bin neu bei JAXB und habe mich gefragt, ob irgendjemand sehen könnte, was ich falsch mache oder ob mich jemand in die richtige Richtung schubsen könnte.

Antwort

1

Sie gerade benötigen, sollten @XmlElement(name="Barge") wie durch die JAXB (JSR-222) Standardbenennungsregeln für JAXB Implementierungen für Elemente mit dem Namen barge statt Barge aussehen wird hinzuzufügen:

@XmlElement(name="Barge") 
public List<Barge> getBarge() { 
    return barge; 
} 

Weitere Informationen

+0

Das hat es geschafft! Vielen Dank! –

0

Definieren Sie einfach die JAXB @ XmlElement-Annotation mit dem bevorzugten Namen über dem erforderlichen Feldsetter. Ich denke auch, dass das Generieren von Klassen aus XML-Schema eine viel bessere Option ist, als den Mapping-Vertrag manuell zu sichern.