现在,我有两个这样的Java类。
public class HogeDomain {
private User userDomain;
public HogeDomain() {
}
and getter/setter..
}
public class User {
public User() {
}
private String id;
private String password;
private Date userDate;
and getter/setter..
}
然后,我在上面创建了一个xml架构,自动使用Ant任务的“Schemagen”。
就是这样。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="hogeDomain">
<xs:sequence>
<xs:element name="userDomain" type="user" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="user">
<xs:sequence>
<xs:element name="id" type="xs:string" minOccurs="0"/>
<xs:element name="password" type="xs:string" minOccurs="0"/>
<xs:element name="userDate" type="xs:dateTime" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
但我真的想创建一个像这样的xml架构来使用JAXB编组或解组。
<?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="hogeDomain">
<xs:complexType>
<xs:sequence>
<xs:element ref="userDomain" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="userDomain">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:string" minOccurs="0"/>
<xs:element name="password" type="xs:string" minOccurs="0"/>
<xs:element name="userDate" type="xs:dateTime" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
如何使用“Schemagen”Ant任务创建此xml架构? 我不想为手工制作一个xml架构。
如果不能解决问题吗?
答案 0 :(得分:1)
您可以执行以下操作:
如下所示:
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement
@XmlType(name="")
public class HogeDomain {
private User userDomain;
@XmlElementRef
public User getUserDomain() {
return userDomain;
}
public void setUserDomain(User userDomain) {
this.userDomain = userDomain;
}
}
和
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement
@XmlType(name="")
public class User {
}
生成以下架构:
<?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="hogeDomain">
<xs:complexType>
<xs:sequence>
<xs:element ref="user"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="user">
<xs:complexType>
<xs:sequence/>
</xs:complexType>
</xs:element>
</xs:schema>
使用以下代码:
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(HogeDomain.class);
jc.generateSchema(new MySOR());
}
private static class MySOR extends SchemaOutputResolver {
@Override
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
StreamResult result = new StreamResult(System.out);
result.setSystemId(suggestedFileName);
return result;
}
}
}