• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unmarshall and Marshall back to XML yeild different result

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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>



The message does kinda tell you that anyType isn't working for you.
I suspect it is because you are depending upon XmlSchema types
getting merged into the default namespace. Off the top of my head
I can't see why that is a problem, but I don't tend to troll through
the namespace spec docs either. Try changing to:

xmlns:xs="http://www.w3.org/2001/XMLSchema"

which you'll find is a bit more typical when people use JAXB
or XmlBeans. After that, xs : dateTime should do the trick for the type if
you still have problems with xs : anyType.
[ January 12, 2006: Message edited by: Reid M. Pinchback ]
 
agilan palani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.
I cannot change the AnyType to DateTime, as Iam user of the schema provided by the webservice provider. However, you are very right is saying AnyType doen't work for me. Other types work very well.
In the example above Anytype cannot be replaced by the datetime, because the Test.xsd contains a anonymous completype as a replacement for the anytype, not a simpletype like DateTime
reply
    Bookmark Topic Watch Topic
  • New Topic