1
votes

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:

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

I've already checked similar topics and I haven't found any solution yet.

Thanks in advance.

1
What do you mean by "to generate in Java"? A simple XSD can be written in an editor - far from rocket science. - laune
@JordiCastilla It seems OP doesn't - laune
I want to know which will be the XSD file because I tried with several xsd files and I didn't found any which generate the given output - user3698770
Well, simply ask for the XSD file and I'll try. - laune

1 Answers

2
votes

This is xxx.xsd, defining the outer element in the foo namespace:

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

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

 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.