Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8502671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:33:00+00:00 2026-06-11T01:33:00+00:00

I have to unmarshal and process XML from an external system over which I

  • 0

I have to unmarshal and process XML from an external system over which I have no control. The XML is in what I believe is the Russian doll format which does not make it easy to process.

The XML share common types which are declared in a common XSD file. But when the JABX generates JAVA classes for these types each outer class contains nested declarations of the common types which makes them different types as far as JAVA is concerned.

I would like to have common JAVA functions to process the common nested types in the XML, but this does not work as in the JAXB classes these common types are unrelated.

This making my JAVA code which needs to process these common types very messy and I’ve been trying to use JAXB bindings to fix the problem. But I have has no success. So my question is:
Given this format of XML/XSD can JAVA code for common types be generated via bindings or some other method?

So an example;

There are two classes in XML CLASSA & CLASSB. Both contain a complex type with one value of type testTYPE which is a string. The XSDs are:

CLASSA.XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:nstype="http://www.test.com/nstypes"
elementFormDefault="qualified">
<xs:import namespace="http://www.test.com/nstypes" schemaLocation="nstypes.xsd"/>
    <xs:element name="CLASSA">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Prop" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="PROP" minOccurs="0">
                                <xs:complexType>
                                    <xs:sequence>   
                                        <xs:element name="Value" type="nstype:testTYPE" minOccurs="0"/>
                                    </xs:sequence>  
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>  
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    </xs:schema>

class B has an idential structure, just a different name.

CLASSB.XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:nstype="http://www.test.com/nstypes"
elementFormDefault="qualified">
<xs:import namespace="http://www.test.com/nstypes" schemaLocation="nstypes.xsd"/>
    <xs:element name="CLASSB">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Prop" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="PROP" minOccurs="0">
                                <xs:complexType>
                                    <xs:sequence>   
                                        <xs:element name="Value" type="nstype:testTYPE" minOccurs="0"/>
                                    </xs:sequence>  
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>  
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    </xs:schema>

The only element called Value of type nstype:testTYPE is in declared in nstypes.xsd. It’s actually a BASETYPE which is a String.

nstypes.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:nstypes="http://www.test.com/nstypes"
targetNamespace="http://www.test.com/nstypes"
elementFormDefault="unqualified">
<xs:complexType name="BASETYPE">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="TYPE" fixed="BASETYPE"/>
            <xs:attribute name="derived" use="optional"/>
            <xs:attribute name="readonly" use="optional"/>
            <xs:attribute name="required" use="optional"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>
<xs:complexType name="testTYPE">
    <xs:simpleContent>
        <xs:extension base="nstypes:BASETYPE"/>
    </xs:simpleContent>
</xs:complexType>
</xs:schema>

The XML for a CLASSA and a CLASSB are similar

ClassA.xml

<?xml version="1.0" encoding="UTF-8"?>
<CLASSA>
    <Prop>
        <PROP>
            <Value>AAA</Value>
        </PROP>
    </Prop>     
</CLASSA>

ClassB.xml

<?xml version="1.0" encoding="UTF-8"?>
<CLASSB>
    <Prop>
        <PROP>
            <Value>BBB</Value>
        </PROP>
    </Prop>     
</CLASSB>

Once the JAXB classes have been generated I’d like to write some code like this:

testJAXB

import java.io.InputStream;

import org.generated.CLASSA.CLASSA;
import org.generated.CLASSA.TestTYPE;
import org.generated.CLASSB.CLASSB;

public class testJAXB
{
    // there is a util function     public static Object loadXML(String xmlFile, String fileName) throws Exception;

    public static void main(String[] args) throws Exception
    {
        // create a ClassA & a ClassB
        CLASSA anAClass = (CLASSA) loadXML("C:\\input\\ClassA.xml", "CLASSA");
        CLASSB anBClass = (CLASSB) loadXML("C:\\input\\ClassB.xml", "CLASSB");

        static void printClass(TestTYPE v)
        {
            // as CLASSA.TestTYPE is imported so v is a CLASSA.TestTYPE
            System.out.println(v.toString());
        }

        // this call will work as there is a printClass which takes a CLASSA.TestTYPE
        printClass(anAClass.getProp().getPROP().getValue());

        // this call will not compile  becase there is no printClass which takes a CLASSA.TestTYPE
        printClass(anBClass.getProp().getPROP().getValue());

        System.out.println("complete.");
    }
}   

What I’d actually like to do is have one implementation of the function printClass(). I guess this would have to take an org.generated.TestTYPE and all the JAXB classes would be generated with org.generated.TestTYPEs ratehr than org.generated.CLASSA.TestTYPEs.
I’m hoping that can be achieved with some bindings magic. If anyone can point me in the right direction that would be much appreciated.

I’m have C++ rather than JAVA background so apologies if some of my terminology is incorrect.

Jérôme,

That’s what I’d like to see but from xjc I see in CLASSA

public CLASSA.Prop getProp()

where CLASSA.Prop is static class which contains a

protected CLASSA.Prop.PROP  

which is another static class which contains a

 protected TestTYPE value;

and TestTYPE is a

org.generated.CLASSA.TestTYPE   

the TestTYPE in CLASSB is a

org.generated.CLASSB.TestTYPE

as the two TestTYPE are nested in different classes they are different types.

essentially, when running xjc I get two files containing the TestTYPE class:

CLASSA/TestTYPE.JAVA

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.09.07 at 07:45:23 AM BST 
//

package org.generated.CLASSA;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
* <p>Java class for testTYPE complex type.
* 
* <p>The following schema fragment specifies the expected content contained within this class.
* 
* <pre>
* &lt;complexType name="testTYPE">
*   &lt;simpleContent>
*     &lt;extension base="&lt;http://www.test.com/nstypes>BASETYPE">
*     &lt;/extension>
*   &lt;/simpleContent>
* &lt;/complexType>
* </pre>
* 
* 
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "testTYPE")
public class TestTYPE
    extends BASETYPE
{


}

and 

CLASSB/TestTYPE.JAVA

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.09.07 at 07:45:23 AM BST 
//

package org.generated.CLASSB;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
* <p>Java class for testTYPE complex type.
* 
* <p>The following schema fragment specifies the expected content contained within this class.
* 
* <pre>
* &lt;complexType name="testTYPE">
*   &lt;simpleContent>
*     &lt;extension base="&lt;http://www.test.com/nstypes>BASETYPE">
*     &lt;/extension>
*   &lt;/simpleContent>
* &lt;/complexType>
* </pre>
* 
* 
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "testTYPE")
public class TestTYPE
    extends BASETYPE
{


}   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:33:02+00:00Added an answer on June 11, 2026 at 1:33 am

    I do not think that JAXB will factorise such a code.
    IMO the easier solution is to write the code of the class Prop and define in the binding file that both B.Prop and A.Prop are relying on the same class.

    For instance using the classImpl attribute.
    http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html#wp148576

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml file which I want to unmarshal. It is a very
I'm trying to unmarshal a simple xml document from a public api from Convio.
I have a relatively simple package of 8 Java classes generated from an XML
I have a servlet which uses JAXB to unmarhsal and marshal an XML file,
I'd like to take a xml file from my classpath to unmarshal it and
I have a class that unmarshals xml from a 3rd party source (I have
I have the following XML structure, which is modelling a single concept across multiple
I'm currently attempting to unmarshal some existing XML into a few classes I have
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.