给定给定的xsd架构:
<xsd:complexType name="webCheckType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="baseUrl" />
<xsd:element name="beginAt" />
<xsd:element name="gotoPage" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="required" />
<xsd:attribute name="extends" type="xsd:IDREF" use="optional" />
<xsd:attribute name="displayName" type="xsd:string" use="required" />
</xsd:complexType>
Xjc会产生这样的东西:
@XmlElements({
@XmlElement(name = "baseUrl", type = BaseUrl.class),
@XmlElement(name = "beginAt", type = BeginAt.class),
@XmlElement(name = "gotoPage", type = GotoPage.class)
})
protected List<Object> baseUrlOrBeginAtOrGotoPage;
如何在binding.xjb
文件中更改此属性名称?
答案 0 :(得分:1)
使用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>
这里我已将list属性的名称更改为“number”
当我使用此绑定运行xjc时,我得到以下Customer类,其中list属性已正确更新
@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中我没有给出这些元素的类型,但在你的情况下应该没问题。