Hi,
Iam unmarshalling input.xml to an
java object and marshal it back to xml
string. Iam not getting the same result. Any light on this? Is is because of the anyType? If I validate the unmarshalled object it throws an error, but if i dont validate, it dosnt give any error. But the result is not same as the input.
----------
PTest.xsd
----------
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://test.com"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://test.com"
elementFormDefault="qualified">
<complexType name="POuterType" abstract="true">
<sequence>
<choice>
<element name="Inner1" type="anyType" />
<element name="Inner2" type="anyType"/>
</choice>
</sequence>
</complexType>
</schema>
---------
Test.xsd
---------
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://test.com"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://test.com" elementFormDefault="qualified" >
<include schemaLocation="PTest.xsd"></include>
<complexType name="OuterType">
<complexContent>
<restriction base="tns
OuterType">
<sequence>
<choice>
<element name="Inner1">
<complexType>
<sequence>
<element name="TimeStamp" type="dateTime">
</element>
</sequence>
</complexType>
</element>
<element name="Inner2">
<complexType>
<sequence>
<element name="TimeStamp" type="dateTime">
</element>
</sequence>
</complexType>
</element>
</choice>
</sequence>
</restriction>
</complexContent>
</complexType>
<element name="Outer" type="tnsuterType"></element>
</schema>
--------
Test.xml
--------
<?xml version="1.0" encoding="UTF-8"?>
<tnsuter xmlns:tns="http://test.com"
xmlnssi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://test.com Test.xsd ">
<tns:Inner1>
<tns:TimeStamp>2001-12-31T12:00:00</tns:TimeStamp>
</tns:Inner1>
</tnsuter>
---------
Main.java
----------
public static void main(String[] args) throws Exception {
JAXBContext ctx = JAXBContext.newInstance("test.vo" );
Unmarshaller unmarsh = ctx.createUnmarshaller();
Marshaller marsh = ctx.createMarshaller();
marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
marsh.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,"http://test.com Test.xsd");
Validator validator = ctx.createValidator();
//Create an Object
ObjectFactory obj = new ObjectFactory();
Outer out =
(Outer)unmarsh.unmarshal(
new FileInputStream( "e:/agilan/JAXBTest/JAXBTe/references/XMLSchema/test/Test.xml" ) );
StringWriter sw2 = new StringWriter();
marsh.marshal(out, new PrintWriter(sw2) );
StringBuffer sb2 = sw2.getBuffer();
System.out.println(sb2.toString() );
}
----------------
Result
------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Outer xsi:schemaLocation="http://test.com Test.xsd" xmlnssi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://test.com">
<Inner1>
</Inner1>
</Outer>
-------------
Please not the timestamp tag is missing, which is present in the input.xml
Iam struck up with is problem for the past 5days, without any clue. Please help.
Additionally if I add the validation code after unmarsh.unmarshal(..).
Iam getting the following exception
Exception in
thread "main" com.sun.xml.bind.serializer.AbortSerializationException: tag name "test.vo.AnyType" is not allowed. Possible tag names are: <test.vo.OuterType.Inner1Type>
at test.vo.impl.runtime.ValidationContext.reportEvent(ValidationContext.java:199)
at test.vo.impl.runtime.ValidationContext.reportEvent(ValidationContext.java:166)
at test.vo.impl.runtime.MSVValidator.childAsElementBody(MSVValidator.java:336)
at test.vo.impl.runtime.MSVValidator.childAsBody(MSVValidator.java:292)
at test.vo.impl.POuterTypeImpl.serializeBody(POuterTypeImpl.java:61)
at test.vo.impl.OuterTypeImpl.serializeBody(OuterTypeImpl.java:30)
at test.vo.impl.runtime.MSVValidator._validate(MSVValidator.java:102)
at test.vo.impl.runtime.MSVValidator.validate(MSVValidator.java:77)
at test.vo.impl.runtime.ValidationContext.validate(ValidationContext.java:75)
at test.vo.impl.runtime.MSVValidator.childAsElementBody(MSVValidator.java:366)
at test.vo.impl.runtime.MSVValidator.childAsBody(MSVValidator.java:292)
at test.vo.impl.OuterImpl.serializeBody(OuterImpl.java:87)
at test.vo.impl.runtime.MSVValidator._validate(MSVValidator.java:102)
at test.vo.impl.runtime.MSVValidator.validate(MSVValidator.java:77)
at test.vo.impl.runtime.ValidationContext.validate(ValidationContext.java:75)
at test.vo.impl.runtime.ValidatorImpl.validate(ValidatorImpl.java:121)
at test.vo.impl.runtime.ValidatorImpl.validateRoot(ValidatorImpl.java:95)
at client.Main.main(Main.java:72)
--------------- linked to ------------------
javax.xml.bind.ValidationException
- with linked exception:
[com.sun.xml.bind.serializer.AbortSerializationException: tag name "test.vo.AnyType" is not allowed. Possible tag names are: <test.vo.OuterType.Inner1Type>]
at test.vo.impl.runtime.ValidatorImpl.validate(ValidatorImpl.java:131)
at test.vo.impl.runtime.ValidatorImpl.validateRoot(ValidatorImpl.java:95)
at client.Main.main(Main.java:72)
DefaultValidationEventHandler: [ERROR]: tag name "test.vo.AnyType" is not allowed. Possible tag names are: <test.vo.OuterType.Inner1Type>
Location: obj: test.vo.impl.OuterTypeImpl@f84386
Iam badly struckup with this problem for the past 5 days please help
[ January 12, 2006: Message edited by: agilan palani ]
[ January 12, 2006: Message edited by: agilan palani ]