gpt4 book ai didi

java - jaxb:将属性绑定(bind)到元素

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

如何使用 JAXB 实现此 XML?目前,grantA、C、B 元素中的 expires 属性没有限制——但它应该有限制。我不知道如何将元素与属性相关联。我是否必须为每个 grantA、C、B 创建类?
XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customers>
<customer>
<name>C1</name>
<grantA expires="">false</grantA>
<grantB expires="">true</grantB>
<grantC expires="">true</grantC>
</customer>
<customer>
<name>C2</name>
<grantA expires="">false</grantA>
<grantB expires="">true</grantB>
<grantC expires="">true</grantC>
</customer>
<customer>
<name>C3</name>
<grantA expires="">false</grantA>
<grantB expires="">true</grantB>
<grantC expires="">false</grantC>
</customer>
</customers>

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="name" type="xs:string" />

<xs:element name="grantA" type="xs:boolean" />
<xs:element name="grantB" type="xs:boolean" />
<xs:element name="grantC" type="xs:boolean" />

<xs:element name="customers" type="customers" />
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
<xs:element ref="name" />
<xs:element name="grantA">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="expires" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="grantB">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="expires" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="grantC">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="expires" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="customers">
<xs:sequence>
<xs:element ref="customer" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>

我现在的状态是:

客户.java:

@XmlRootElement(name = "customers")
public class Customers
{
private List<Customer> customerList;

@XmlElement(name = "customer")
public List<Customer> getCustomerList()
{
if (null == customerList)
{
customerList = new ArrayList<Customer>();
}
return customerList;
}

public void setCustomerList(List<Customer> customers)
{
this.customerList = customers;
}

}

客户.java

@XmlRootElement(name = "customer")
@XmlType(propOrder = { "name", "grantA", "grantB", "grantC" })
public class Customer
{
private String name = "";
private boolean grantA;
private boolean grantB;
private boolean grantC;

@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlElement(name = "name", required=true)
public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

@XmlElement(name = "grantA", required=true)
public boolean isGrantA()
{
return grantA;
}

public void setGrantA(boolean grantA)
{
this.grantA = grantA;
}



@XmlElement(name = "grantB", required=true)
public boolean isGrantB()
{
return grantB;
}

public void setGrantB(boolean grantB)
{
this.grantB = grantB;
}

@XmlElement(name = "grantC", required=true)
public boolean isGrantC()
{
return grantC;
}

public void setGrantC(boolean grantC)
{
this.grantC = grantC;
}


@Override
public String toString()
{
return "" + name + "[ " + grantA + ", " + grantB + " " + grantC + " ]";
}

}

最佳答案

您可以创建一个类 Grant,如下所示:

@XmlType
public class Grant {
@XmlAttribute
private String expires;

@XmlValue
private boolean value;

//getters and setters
}

并在 Customer 类中用不同的名称映射它,如下所示:

public class Customer
{
private String name = "";
@XmlElement(name="grantA");
private Grant grantA;

@XmlElement(name="grantB");
private Grant grantB;

@XmlElement(name="grantC");
private Grant grantC;

//rest of the code
}

关于java - jaxb:将属性绑定(bind)到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12260259/

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