Page 1 of 1

java web servicex jaxb Unmarshalling

PostPosted: Thu May 12, 2011 12:02 pm
by haretu
Unmarshalling

The Unmarshaller class in the javax.xml.bind package provides the client application the ability to convert XML data into a tree of Java content objects. The unmarshal method for a schema (within a namespace) allows for any global XML element declared in the schema to be unmarshalled as the root of an instance document. The JAXBContext object allows the merging of global elements across a set of schemas (listed in the contextPath). Since each schema in the schema set can belong to distinct namespaces, the unification of schemas to an unmarshalling context should be namespace-independent. This means that a client application is able to unmarshal XML documents that are instances of any of the schemas listed in the contextPath; for example:

JAXBContext jc = JAXBContext.newInstance(
"com.acme.foo:com.acme.bar" );

Unmarshaller u = jc.createUnmarshaller();

FooObject fooObj =
(FooObject)u.unmarshal( new File( "foo.xml" ) ); // ok

BarObject barObj =
(BarObject)u.unmarshal( new File( "bar.xml" ) ); // ok

BazObject bazObj =
(BazObject)u.unmarshal( new File( "baz.xml" ) );
// error, "com.acme.baz" not in contextPath
A client application may also generate Java content trees explicitly rather than unmarshalling existing XML data. To do so, the application needs to have access and knowledge about each of the schema-derived ObjectFactory classes that exist in each of Java packages contained in the contextPath. For each schema-derived Java class, there will be a static factory method that produces objects of that type. For example, assume that after compiling a schema, you have a package com.acme.foo that contains a schema-derived interface named PurchaseOrder. To create objects of that type, the client application would use the following factory method:

ObjectFactory objFactory = new ObjectFactory();

com.acme.foo.PurchaseOrder po =
objFactory.createPurchaseOrder();