ps-aux
ps-aux

Reputation: 12176

Shared class for child element in JAXB in different xmls/roots

In JAXB when using automatic class generation via xjc from xsd scheme.

alpha.xsd

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="alpha">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="persons">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="person" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="name"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

beta.xml

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="country">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="class">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="person">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="name"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

As you can see there is Person element which is shared among these two schemes. What I would like to do is:

The purpose of this is unmarshalling one xml, applying business logic and creating another one as output where some elements (like Person) are same and shared for both xml files. The namespace will be the same for both files.

I would welcome if you can present me with complete .xjb bindings settings file. So far mine contains only:

<jxb:bindings version="1.0" 
  xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
  jxb:extensionBindingPrefixes="xjc">

  <jxb:globalBindings localScoping="toplevel"/>
</jxb:bindings>

And of course I get name collision error as I do not know how to set binding compiler to see Person as the same entity/element.

Upvotes: 4

Views: 2537

Answers (2)

bdoughan
bdoughan

Reputation: 149037

You can use an external binding file to indicate that during class generation we wish to use our existing class for the complex type called Document.

binding.xml

<jxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">

    <jxb:bindings schemaLocation="beta.xsd">
        <jxb:bindings node="//xs:element[@name='person']/complexType">
            <jxb:class ref="alpha.Person"/>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

XJC Call

xjc -b binding.xml beta.xsd

Upvotes: 6

Sergey Morozov
Sergey Morozov

Reputation: 4628

If namespace of person from A will be equals namespace person from B, that xjc has to generate the correct classes.

Upvotes: 1

Related Questions