使用 JAXB 绑定定制,您可以使用 XPath 指定具有特定名称的节点并定制其属性名称。代码将与此类似(未经测试,但您明白了)
<jxb:bindings node=".//xs:element[@name='name']">
<jxb:property name="toName"/>
</jxb:bindings>
这是我的示例 xsd - customer.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="customer">
<xsd:complexType>
<xsd:sequence>
<xsd:element
name="phone-number"
type="xsd:string"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
在此架构上运行 xjc 会创建以下类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"phoneNumber"
})
@XmlRootElement(name = "customer")
public class Customer {
@XmlElement(name = "phone-number", required = true)
protected List<String> phoneNumber;
public List<String> getPhoneNumber() {
if (phoneNumber == null) {
phoneNumber = new ArrayList<String>();
}
return this.phoneNumber;
}
现在我创建了一个自定义绑定
<jxb:bindings schemaLocation="customer.xsd">
<jxb:bindings node="//xs:element[@name='customer']/xs:complexType/xs:sequence/xs:element[@name='phone-number']">
<!--<jxb:property name="" collectionType="java.util.LinkedList"/>-->
<jxb:property name="number"/>
</jxb:bindings>
</jxb:bindings>
在这里,我将列表属性的名称更改为“数字”
当我使用此绑定运行 xjc 时,我得到了以下具有正确更新列表属性的 Customer 类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"number"
})
@XmlRootElement(name = "customer")
public class Customer {
@XmlElement(name = "phone-number", required = true)
protected List<String> number;
public List<String> getNumber() {
if (number == null) {
number = new ArrayList<String>();
}
return this.number;
}
使用您的架构,您可以尝试按照以下方式给出您自己的属性名称
<jxb:bindings node="//xs:complexType/xs:sequence[xs:element[@name='baseUrl' or @name='beginAt' or @name='gotoPage']]">
<jxb:property name="yourpropertyname"/>
</jxb:bindings>
在您的架构上运行它会给出
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "webCheckType", propOrder = {
"number"
})
public class WebCheckType {
@XmlElementRefs({
@XmlElementRef(name = "beginAt", type = JAXBElement.class),
@XmlElementRef(name = "gotoPage", type = JAXBElement.class),
@XmlElementRef(name = "baseUrl", type = JAXBElement.class)
})
protected List<JAXBElement<Object>> number;
@XmlAttribute(required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlID
@XmlSchemaType(name = "ID")
protected String id;
@XmlAttribute(name = "extends")
@XmlIDREF
@XmlSchemaType(name = "IDREF")
protected Object _extends;
@XmlAttribute(required = true)
protected String displayName;
........................
.......................
public List<JAXBElement<Object>> getNumber() {
if (number == null) {
number = new ArrayList<JAXBElement<Object>>();
}
return this.number;
}
当然在我的情况下它的 JAXBElement 因为在我的 xsd 中我没有给出这些元素的类型,但在你的情况下应该没问题。