XSD 来自 XML Java Jaxb
XSD from XML Java Jaxb
我需要在 Java 中生成 xsd 文件,使用 jaxb maven 插件 (http://mojo.codehaus.org/jaxb2-maven-plugin/xjc-mojo.html) 将生成 XML 如下所示:
<data xmlns = "http://foo.com">
<childData xmlns = "http://bar.com" />
</data>
我不想编辑 jaxb 自动生成的 类 或类似的东西。
我已经检查过类似的主题,但我还没有找到任何解决方案。
提前致谢。
这是xxx.xsd,定义foo命名空间中的外部元素:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:foo="http://foo.com"
targetNamespace="http://foo.com"
xmlns:bar="http://bar.com"
jaxb:version="2.0">
<xsd:import namespace="http://bar.com"
schemaLocation="yyy.xsd"/>
<xsd:complexType name="DataType">
<xsd:sequence>
<xsd:element ref="bar:childData"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="data" type="foo:DataType"/>
</xsd:schema>
这里是 yyy.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
targetNamespace="http://bar.com"
xmlns:bar="http://bar.com"
jaxb:version="2.0">
<xsd:element name="childData" type="xsd:string"/>
</xsd:schema>
以后编组的常用Java代码:
void marshal() throws Exception {
JAXBContext jc = JAXBContext.newInstance( "com.foo:com.bar" );
Marshaller m = jc.createMarshaller();
DataType data = new DataType();
ObjectFactory of = new ObjectFactory();
JAXBElement<DataType> jbe = of.createData(data);
data.setChildData("child data");
m.marshal( jbe, System.out );
}
生产
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:data xmlns="http://bar.com" xmlns:ns2="http://foo.com">
<childData>child data</childData>
</ns2:data>
相当于您发布的XML。
我需要在 Java 中生成 xsd 文件,使用 jaxb maven 插件 (http://mojo.codehaus.org/jaxb2-maven-plugin/xjc-mojo.html) 将生成 XML 如下所示:
<data xmlns = "http://foo.com">
<childData xmlns = "http://bar.com" />
</data>
我不想编辑 jaxb 自动生成的 类 或类似的东西。
我已经检查过类似的主题,但我还没有找到任何解决方案。
提前致谢。
这是xxx.xsd,定义foo命名空间中的外部元素:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:foo="http://foo.com"
targetNamespace="http://foo.com"
xmlns:bar="http://bar.com"
jaxb:version="2.0">
<xsd:import namespace="http://bar.com"
schemaLocation="yyy.xsd"/>
<xsd:complexType name="DataType">
<xsd:sequence>
<xsd:element ref="bar:childData"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="data" type="foo:DataType"/>
</xsd:schema>
这里是 yyy.xsd:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
targetNamespace="http://bar.com"
xmlns:bar="http://bar.com"
jaxb:version="2.0">
<xsd:element name="childData" type="xsd:string"/>
</xsd:schema>
以后编组的常用Java代码:
void marshal() throws Exception {
JAXBContext jc = JAXBContext.newInstance( "com.foo:com.bar" );
Marshaller m = jc.createMarshaller();
DataType data = new DataType();
ObjectFactory of = new ObjectFactory();
JAXBElement<DataType> jbe = of.createData(data);
data.setChildData("child data");
m.marshal( jbe, System.out );
}
生产
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:data xmlns="http://bar.com" xmlns:ns2="http://foo.com">
<childData>child data</childData>
</ns2:data>
相当于您发布的XML。