gpt4 book ai didi

xml - JaxB 使用大写字母标签解码 XML

转载 作者:行者123 更新时间:2023-12-04 00:47:02 28 4
gpt4 key购买 nike

我使用 Sprint 3.0.5 和包含的 jaxb 编码器与 REST 服务通信。另一家公司提供的服务通过 POST 将 XML 发送到我的服务,我必须在我的 Java 对象中解码该 XML 并处理它们。

我遇到的问题是,XML 标记以大写字母开头(他们不会更改它),因此 JAXB 编码器无法解码对象。

XML 如下所示:

<Ssm_Packet>
<Version>1</Version>
<Protocol_ID>0</Protocol_ID>
<Packet_Id>{84ca597c-05e2-4357-897c-892f428c35ce}</Packet_Id>
<Priority>0</Priority>
<Source_Address>1:11111111111111</Source_Address>
<Destination_Address>2:LA3222222222222</Destination_Address>
<Body>Some TExt</Body>
<Billing />
</Ssm_Packet>

我为 jaxb 定义的 bean 如下所示:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="Ssm_Packet")
public class Ssm_Packet {
@XmlElement(name="Version")
private String version;
private String protocol_ID;
private String packet_Id;
private String priority;
private String source_Address;
private String destination_Address;
private String body;
private String billing;
/**
* @return the version
*/
public String getVers() {
return version;
}
/**
* @param version the version to set
*/
public void setVers(String Version) {
this.version = Version;
}
/**
* @return the protocol_ID
*/
public String getProtocol_ID() {
return protocol_ID;
}
/**
* @param protocol_ID the protocol_ID to set
*/
public void setProtocol_ID(String Protocol_ID) {
this.protocol_ID = Protocol_ID;
}
/**
* @return the packet_Id
*/
public String getPacket_Id() {
return packet_Id;
}
/**
* @param packet_Id the packet_Id to set
*/
public void setPacket_Id(String Packet_Id) {
this.packet_Id = Packet_Id;
}
/**
* @return the priority
*/
public String getPriority() {
return priority;
}
/**
* @param priority the priority to set
*/
public void setPriority(String Priority) {
this.priority = Priority;
}
/**
* @return the source_Address
*/
public String getSource_Address() {
return source_Address;
}
/**
* @param source_Address the source_Address to set
*/
public void setSource_Address(String Source_Address) {
this.source_Address = Source_Address;
}
/**
* @return the destination_Address
*/
public String getDestination_Address() {
return destination_Address;
}
/**
* @param destination_Address the destination_Address to set
*/
public void setDestination_Address(String Destination_Address) {
this.destination_Address = Destination_Address;
}
/**
* @return the body
*/
public String getBody() {
return body;
}
/**
* @param body the body to set
*/
public void setBody(String Body) {
this.body = Body;
}
/**
* @return the billing
*/
public String getBilling() {
return billing;
}
/**
* @param billing the billing to set
*/
public void setBilling(String Billing) {
this.billing = Billing;
}
}

现在,如果我让 JAXb 解码该对象中的 XML,他将不会填写 xml 的值,除非 xml 标签没有大写字母。

任何人都可以帮助我如何将这些值解码到我的 bean 中吗?

谢谢

最佳答案

您可以使用 @XmlElement 注释来指定与您的每个字段/属性相对应的元素名称。

/**
* @return the protocol_ID
*/
@XmlElement(name="Protocol_ID")
public String getProtocol_ID() {
return protocol_ID;
}

如果您想改为注释字段,则需要设置 @XmlAccessorType(XmlAccessType.FIELD):

@XmlRootElement(name="Ssm_Packet")
@XmlAccessorType(XmlAccessType.FIELD)
public class Ssm_Packet {
@XmlElement(name="Version")
private String version;
}

EclipseLink JAXB (MOXy)还包含一个扩展,您可以在其中覆盖用于将 Java 字段/属性名称转换为 XML 名称的标准 JAXB 算法:

关于xml - JaxB 使用大写字母标签解码 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7417607/

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