gpt4 book ai didi

java - 使用jaxb读取xml文档

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:35 30 4
gpt4 key购买 nike

我正在尝试使用 jaxb 读取 xml,并尝试创建一个解析器。我对我构建 java bean 的方式及其中的注释有些怀疑。我知道我做错了,因为我遇到了非法注释错误。

关于如何编写 bean 和注释以允许 jax b 执行它并解析我的 xml,我能否获得一些帮助。

谢谢(很抱歉发了这么长的帖子,但希望它能对其他人有所帮助)

我的简化 xml 文档看起来像这样......

<WEB-ITM-EXT-REC>
<ACTVTY-CODE>A</ACTVTY-CODE>
<WEB-ITM-EXT-DATA>
<ITEM-NO>93501250080</ITEM-NO>
<PROMOTION-DETAILS>
<PROMOTION-ID></PROMOTION-ID>
<PROMOTION-START-DATE>0</PROMOTION-START-DATE>
<PROMOTION-END-DATE>0</PROMOTION-END-DATE>
<PROMOTION-PRICE>.00</PROMOTION-PRICE>
</PROMOTION-DETAILS>
<NORMAL-SELL-PRICES>
<SELL-PRICE-EFFECTIVE-DATE-1>0</SELL-PRICE-EFFECTIVE-DATE-1>
<SELL-PRICE-1>.00</SELL-PRICE-1>
<SELL-PRICE-EFFECTIVE-DATE-2>0</SELL-PRICE-EFFECTIVE-DATE-2>
<SELL-PRICE-2>.00</SELL-PRICE-2>
<SELL-PRICE-EFFECTIVE-DATE-3>0</SELL-PRICE-EFFECTIVE-DATE-3>
<SELL-PRICE-3>189.95</SELL-PRICE-3>
</NORMAL-SELL-PRICES>
<OUT-OF-STOCK-IND>Y</OUT-OF-STOCK-IND>
</WEB-ITM-EXT-DATA>
</WEB-ITM-EXT-REC>

这是我编写 java bean 的方式...

@XmlRootElement(name = "WEB-ITM-EXT-REC")
public class ItemDocuments {

@XmlElement(name="ACTVTY-CODE")
private String activityCode;

@XmlElement(name="WEB-ITM-EXT-DATA")
private List<ItemDocument> itemDocuments;

public List<ItemDocument> getItemDocuments() {
return itemDocuments;
}

public void setItemDocuments(List<ItemDocument> itemDocuments) {
this.itemDocuments = itemDocuments;
}

public String getActivityCode() {
return activityCode;
}

public void setActivityCode(String activityCode) {
this.activityCode = activityCode;
}
}

项目 bean

@XmlRootElement(name = "WEB-ITM-EXT-DATA")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemDocument {

@XmlElement(name="ITEM-NO")
private String itemNo;
private PromotionDocument promotion;
private SellPriceDocument sellPrice;
@XmlElement(name="OUT-OF-STOCK-IND")
private String outOfStockInd;

getters..
setters...
}

Bean 售价

@XmlType
public class SellPriceDocument {

@XmlElement(name="SELL-PRICE-EFFECTIVE-DATE-1")
private String sellPriceEffectiveDateOne;
@XmlElement(name="SELL-PRICE-1")
private String sellPriceOne;
@XmlElement(name="SELL-PRICE-EFFECTIVE-DATE-2")
private String sellPriceEffectiveDateTwo;
@XmlElement(name="SELL-PRICE-2")
private String sellPriceTwo;
@XmlElement(name="SELL-PRICE-EFFECTIVE-DATE-3")
private String sellPriceEffectiveDateThree;
@XmlElement(name="SELL-PRICE-3")
private String sellPriceThree;

getters..
setters...
}

促销 bean

public class PromotionDocument {

@XmlElement(name="PROMOTION-ID")
private String promotionId;

@XmlElement(name="PROMOTION-START-DATE")
private String promotionStartDate;

@XmlElement(name="PROMOTION-END-DATE")
private String promotionEndDate;

@XmlElement(name="PROMOTION-PRICE")
private String promotionPrice;

getters..
setters...
}

最佳答案

默认情况下,JAXB 实现会将公共(public)属性视为已映射。这意味着默认情况下您应该注释属性:

@XmlRootElement(name = "WEB-ITM-EXT-REC")
public class ItemDocuments {

private List<ItemDocument> itemDocuments;

@XmlElement(name="WEB-ITM-EXT-DATA")
public List<ItemDocument> getItemDocuments() {
return itemDocuments;
}

public void setItemDocuments(List<ItemDocument> itemDocuments) {
this.itemDocuments = itemDocuments;
}

}

当您注释该字段时,您应该在类上指定 @XmlAccessorType(XmlAccessTypr.FIELD)

@XmlRootElement(name = "WEB-ITM-EXT-REC")
@XmlAccessorType(XmlAccessType.FIELD)
public class ItemDocuments {

@XmlElement(name="WEB-ITM-EXT-DATA")
private List<ItemDocument> itemDocuments;

public List<ItemDocument> getItemDocuments() {
return itemDocuments;
}

public void setItemDocuments(List<ItemDocument> itemDocuments) {
this.itemDocuments = itemDocuments;
}

关于java - 使用jaxb读取xml文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19371587/

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