简体   繁体   English

XML Java Jaxb的XSD

[英]XSD from XML Java Jaxb

I need to generate in Java the xsd file which using jaxb maven plugin ( http://mojo.codehaus.org/jaxb2-maven-plugin/xjc-mojo.html ) will produce an XML like the following: 我需要在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>

I don't want to edit the jaxb autogenerated classes or something like that. 我不想编辑jaxb自动生成的类或类似的东西。

I've already checked similar topics and I haven't found any solution yet. 我已经检查过类似的主题,但尚未找到任何解决方案。

Thanks in advance. 提前致谢。

This is xxx.xsd, defining the outer element in the foo namespace: 这是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>

And here is yyy.xsd: 这是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>

Later The usual Java code for marshalling: 以后用于编组的常用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 );
}

produces 产生

<?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>

which is equivalent to the XML you have posted. 与您发布的XML等效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM