2015-09-29 43 views
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的唯一方式

回答

2

1)使用marshaller.setSchema(schema)設置XML模式;打開對該XML模式的驗證,並且您知道全數字ID值無效。

2)您可以使用適配器。

在Employee.java添加註釋:

@XmlAttribute 
@XmlID 
@XmlJavaTypeAdapter(Adapter.class) 
private String id; 

你會需要這個簡單的類:

public class Adapter extends XmlAdapter<String, String> { 
    @Override 
    public String marshal(String s) throws Exception { 
    return "_" + s; 
    } 
    @Override 
    public String unmarshal(String v) throws Exception { 
    return v.substring(1); 
    } 
} 

生成的XML將根據XML模式是正確的:

<company> 
    <employee id="_1" name="Jane Doe"> 
    <report>_2</report> 
    <report>_3</report> 
    </employee> 
    <employee id="_2" name="John Smith"> 
    <manager>_1</manager> 
    </employee> 
    <employee id="_3" name="Anne Jones"> 
    <manager>_1</manager> 
    </employee> 
</company>