gpt4 book ai didi

java - 来自 XML Java Jaxb 的 XSD

转载 作者:行者123 更新时间:2023-11-30 03:25:02 26 4
gpt4 key购买 nike

我需要在 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 - 来自 XML Java Jaxb 的 XSD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30461544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com