JAX: xsd,xml during generetion of code and during runtime are different
Hi
Below is problem where I am getting stuck..
I created java code using a xsd with suppose 4 string tag. Every thing works fine, when i pass a xml with only the 4 string tag.
Now the xml with 5 string tag, basically a new string tag has been added which was not added in the scehma. As soon as I pass this my JAXB test program fails. It fail due to the extra string tag. This string tag is optional.
Can I still use the same generated jaxb code and the xml with 5 string tag.. without regenrating...
[532 byte] By [
leon_johna] at [2007-11-26 22:17:46]

# 1
Looks like the problem is not explained properly. Will try to add an example.
XSD
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="addressBook">
<xs:complexType>
<xs:sequence>
<xs:element ref="contact" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="email" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML INPUT1 (Works fine)
<?xml version="1.0"?>
<addressBook>
<contact>
<name>John Doe</name>
<email>john@acme.com</email>
</contact>
<contact>
<name>Jane Smith</name>
<email>jane@acme.org</email>
</contact>
<contact>
<name>Bob Cole</name>
<email>bob@acme.net</email>
</contact>
</addressBook>
XML INPUT2 (Has Error as a new tag is added in the xml source but the xsd and generated code remains same.
Note this new tag is optional to us so we do not need it as for now
Is a way I can run my program with this new tag in the xml source ?)
<?xml version="1.0"?>
<addressBook>
<noOfContacts>3</noOfContacts>
<contact>
<name>John Doe</name>
<email>john@acme.com</email>
</contact>
<contact>
<name>Jane Smith</name>
<email>jane@acme.org</email>
</contact>
<contact>
<name>Bob Cole</name>
<email>bob@acme.net</email>
</contact>
</addressBook>
Hope I have explained the problem more clearly..
Hope to get a solution...
Thanks for your time....
Message was edited by:
leon_john
Message was edited by:
leon_john
# 2
The XML INPUT2 document simply does not match the XSD schema. To make the XML INPUT2 document valid, you have to alter the XSD schema (i.e. prepare it to accept the <noOfContacts> element) in some way as in the following examples.
This example demonstrates a simple alteration by adding a single line to the XSD schema:<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="addressBook">
<xs:complexType>
<xs:sequence>
<xs:element name="noOfContacts" minOccurs="0"/>
<xs:element ref="contact" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="email" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This example is a little more complex but is more coherent with the logic of your original XSD schema:<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="addressBook">
<xs:complexType>
<xs:sequence>
<xs:element ref="noOfContacts" minOccurs="0"/>
<xs:element ref="contact" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="noOfContacts">
<xs:simpleType>
<xs:restriction base="xs:integer"/>
</xs:simpleType>
</xs:element>
<xs:element name="contact">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="email" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
So it is not the number of child elements of the <addressBook> element that makes the XML INPUT2 document invalid, but an element (<noOfContacts>) that is not declared in the XSD schema.
# 3
Hi
Thats for sure if I update the xsd and regenerate the java class, everything works.
My scenario is like this.
XSD are maintained by some other group.
They are adding some optional fields, which we do not require.
I need to develop the JAXB classes such that even if they add new optional fields in the XSD and the Xml Response, our end should not break?
Thanks
Leon
# 4
Any Ideas on these... This is practical issue.... Come one jaxb guys help me out.... Or atleast give some hints