这是我的 .xsd 文件
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Person" type="PersonType"/>
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Address" type="AddressType" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="AddressType">
<xs:sequence>
<xs:element name="Number" type="xs:unsignedInt"/>
<xs:element name="Street" type="xs:string"/>
</xs:sequence>
</xs:complexType>
使用这个 XSD 文件我生成了这个类:
package demo5;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PersonType", propOrder = {
"name",
"address"
})
public class PersonType {
@XmlElement(name = "Name", required = true)
protected String name;
@XmlElement(name = "Address", required = true)
protected List<AddressType> address;
public String getName() {
return name;
}
public void setName(String value) {
this.name = value;
}
public List<AddressType> getAddress() {
if (address == null) {
address = new ArrayList<AddressType>();
}
return this.address;
}
}
但 XSD 文件不会在 java 文件中生成 @XMLRootElement。任何人都可以为此提供解决方案。我知道可以生成根元素,但这不起作用。