0
我有以下類別:如何使用數字和空格在xmlID xmlIDREF JAXB
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
@XmlAttribute
@XmlID
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Employee getManager() {
return manager;
}
public void setManager(Employee manager) {
this.manager = manager;
}
public List<Employee> getReports() {
return reports;
}
public void setReports(List<Employee> reports) {
this.reports = reports;
}
@XmlAttribute
private String name;
@XmlIDREF
private Employee manager;
@XmlElement(name="report")
@XmlIDREF
private List<Employee> reports;
public Employee() {
reports = new ArrayList<Employee>();
}
}
公司類:
我主要包含以下內容:
Employee employee1 = new Employee();
employee1.setId("1");
employee1.setName("Jane Doe");
company.getEmployees().add(employee1);
Employee employee2 = new Employee();
employee2.setId("2");
employee2.setName("John Smith");
employee2.setManager(employee1);
employee1.getReports().add(employee2);
company.getEmployees().add(employee2);
Employee employee3 = new Employee();
employee3.setId("3");
employee3.setName("Anne Jones");
employee3.setManager(employee1);
employee1.getReports().add(employee3);
company.getEmployees().add(employee3);
如果我編制它,它運行良好。但是,如果我創建了下面的架構,然後添加架構主然後我收到以下錯誤:
模式:收到
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="company" type="company"/>
<xs:complexType name="company">
<xs:sequence>
<xs:element name="employee" type="employee" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="employee">
<xs:sequence>
<xs:element name="manager" type="xs:IDREF" minOccurs="0"/>
<xs:element name="report" type="xs:IDREF" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID"/>
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
</xs:schema>
錯誤:
異常線程「main」 javax.xml.bind.MarshalException - 與鏈接的異常: [org.xml.sax.SAXParseException:CVC-數據類型-valid.1.2.1: '1' 不是 '的NCName' 的有效值。]
我知道這是因爲NCName不能以數字開頭並且有空格。但在我的情況下,ID是一個數字。
我想知道: 1)爲什麼只有當我包含模式時纔會發生這種情況? 2)什麼是解決方法,並且是使用moxy的唯一方式