XML Unmarshalling problem with jaxb
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   errorken_jaja
Posted On:   Saturday, April 3, 2004 07:13 AM

int i=1; // sample code block I have 2 very simple XSD schema's. SchemaA.xsd: [request]-------[element_A] is xs:string |__(ANY) SchemaB.xsd: [body]--------[element_B] is xs:string The any block says that there can follow any type of xml after the element_a. In the xsd it is coded as: I compiled both schema's with jaxb, using this command line: xjc SchemaA.xsd -p test.request xjc SchemaB.xsd -p test.body -use-runtime test.request.impl.runtime I created the following code : test.request.ObjectFactory of1 = new test.request.ObjectFactory(); test.body.ObjectFactory of2 = new test.body.ObjectFactory();    More>>
			
int i=1; // sample code block
I have 2 very simple XSD schema's.

SchemaA.xsd:

[request]-------[element_A] is xs:string
|__(ANY)

SchemaB.xsd:

[body]--------[element_B] is xs:string


The any block says that there can follow any type of xml after the element_a.
In the xsd it is coded as:

I compiled both schema's with jaxb, using this command line:

xjc SchemaA.xsd -p test.request
xjc SchemaB.xsd -p test.body -use-runtime test.request.impl.runtime

I created the following code :

test.request.ObjectFactory of1 = new test.request.ObjectFactory();
test.body.ObjectFactory of2 = new test.body.ObjectFactory();

Request r = of1.createRequest();
r.setElementA("testing, element one");

Body b = of2.createBody();
b.setElementB("testing, element two");

r.setAny(b);

Marshaller m = of1.createMarshaller();
m.marshal(r,System.out);

And the result is what I expected:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
testing, element one testing, element two

JAXB added the xml from the second schema to the first.
Ok, so far so good. Now I'm trying to do it the other way around, I want to unmarshall that XML to objects again:

String xml <- contains the outputted xml from the marshall (as pasted a few lines above)

Unmarshaller um = of1.createUnmarshaller();
Request r2 = (Request)um.unmarshal(new ByteArrayInputStream(xml.getBytes()));
System.err.println(r2.getElementA());
System.err.println(r2.getAny());

The output:

testing, element one
null

now, the first message is good. The second is not however...why is the "getAny" null ? I can't figure this one out. In the best possible case it should return an object that can be cast to "Body" .
In the worst case it should just return the " ...." xml as string. But not null ...

Any help on this is appreciated !

   <<Less
About | Sitemap | Contact