users@jaxb.java.net

Is it possible to marshall a part of a JAXB object ?

From: François Banel <fbanel_at_LAPOSTE.NET>
Date: Fri, 28 Mar 2003 01:23:30 -0700

Hello,

Is it possible to marshall a part of a JAXB object ?

What I want to do is :
- unmarshall a XML File to a Java Object
- get a subelement of this java object
- marshall this subelement to a XML File

This was working in beta version.
It seems not to work in the release version.

using the release version, The resulting XML File does not
contain the XML corresponding to the subelement, but the XML
corresponding to the CONTENT of the subelement (see example)

Does anyone know how to marshall a part of a JAXB object ?
thanks for any hint



the sample XMLSchema file :
<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.fb.org" xmlns:order="http://www.fb.org" xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

 <complexType name="OrderType">
  <sequence>
   <element name="quantity" type="int"/>
   <element ref="order:subpart"/>
  </sequence>
  <attribute name="id" type="int" use="required"/>
 </complexType>

 <complexType name="Type1">
  <sequence>
   <element name="key" type="int"/>
  </sequence>
  <attribute name="attr" type="string"/>
 </complexType>

 <element name="Order" type="order:OrderType"/>
 <element name="subpart" type="order:Type1"/>
</schema>


the sample XML file (order.xml) :
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSPY v5 rel. 2 U (http://www.xmlspy.com)-->
<Order xmlns="http://www.fb.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fb.org
C:\doc\jaxb\order.xsd" id="0">
 <quantity>0</quantity>
 <subpart attr="test">
  <key>0</key>
 </subpart>
</Order>

the sample Java program :
 JAXBContext context = JAXBContext.newInstance("org.fb");
 Unmarshaller un = context.createUnmarshaller();
 un.setValidating(true);
 Order order = (Order) un.unmarshal(new java.io.File("c:\\doc\\jaxb\\order.xml"));
 Type1 subpartType = order.getSubpart();

 Marshaller m = context.createMarshaller();
 m.marshal(order, new java.io.FileWriter("c:\\doc\\jaxb\\out1.xml"));
 m.marshal(subpartType, new java.io.FileWriter("c:\\doc\\jaxb\\out2.xml"));

the complete marshalling of "order" (out1.xml):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:Order id="0" xmlns:ns1="http://www.fb.org"><ns1:quantity>0</ns1:quantity><ns1:subpart attr="test"><ns1:key>0</ns1:key></ns1:subpart></ns1:Order>

the partial marshalling of "subpartType" (out2.xml):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:key xmlns:ns1="http://www.fb.org">0</ns1:key>