gpt4 book ai didi

java - 将 JAXB 与包含许多相同元素的 XML 文件一起使用

转载 作者:行者123 更新时间:2023-11-30 11:46:01 25 4
gpt4 key购买 nike

我正在创建一个 Java 类来映射具有多个相同元素的 XML 文件。以下 XML 是该文件的示例:

<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 类可以读入 DateTime、From、To、Classification、Command 和 MsgId 元素,但无法读入 Barge 元素。我有两个类试图封装 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 中的所有内容,但 Barges 除外。我是 JAXB 的新手,想知道是否有人可以看到我可能做错了什么,或者是否有人可以将我推向正确的方向。

最佳答案

你应该只需要添加 @XmlElement(name="Barge") 作为 JAXB (JSR-222)默认命名规则 JAXB 实现将查找名称为 barge 而不是 Barge 的元素:

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

了解更多信息

关于java - 将 JAXB 与包含许多相同元素的 XML 文件一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10029228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com