users@jaxb.java.net

Help!! Elements & types

From: <JMedina_at_on.com>
Date: Thu, 7 Aug 2003 09:45:58 +0200

I inititally had a XML schema that allowed as the root element a single
entity ("package"). This package can be composed of other several
subelements and set of attributes. (In my example below, I removed all the
subelements an attributes except one called "classes" and "class").

But, I need to marshall not only elements of the "package". I would like to
be able to produce documents that only contain a subelement. Then, I
modified my schema to allow also the subelements to be a root element.

Nevertheless, I still cannot marshal to a new document a subentity.


Is it possible to marshal to XML a subeentity of a document ?

I want to use the same XML format of the subelements for use in clipboard
operations.

But so far, I can't marshall a subelement correctly. It seems that
eventhough I defined them as valid root elements <xsd:element> they are
treated as "types" when parsing a document containing them (package).

I'm not sure I'm being clear on my question, so I am including some code
and the output I was expecting.

Thanks

-Jorge


I'm getting:

Marshalling a classes:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<class name="test" xmlns="http://www.on.com/xml/schemas"/>
<class name="sample" description="A class description goes here."
xmlns="http://www.on.com/xml/schemas"/>
Marshalling a class:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
Marshalling a class:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

when I expected:


Marshalling a classes:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<classes xmlns="http://www.on.com/xml/schemas">
<class name="test"/>
<class name="sample" description="A class description goes here."/>
</classes>
Marshalling a class:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<class name="test" xmlns="http://www.on.com/xml/schemas"/>
Marshalling a class:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<class name="sample" description="A class description goes here."
xmlns="http://www.on.com/xml/schemas"/>


This is the Java code I have:

jaxbContext = JAXBContext.newInstance(JAXB_PACKAGE,
ClassLoader.getSystemClassLoader());
unmarshaller = jaxbContext.createUnmarshaller();
marshaller = jaxbContext.createMarshaller();
validator = jaxbContext.createValidator();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new
Boolean(true));

PackageElement pkg = (PackageElement) unmarshaller.unmarshal( new
File("d:/projects/java/dsrts/samples/sample_3.xml"));

System.out.println("Marshalling a classes: ");
marshaller.marshal( pkg.getClasses() , System.out );

Iterator iter = pkg.getClasses().getClazz().iterator();
ClassType ct;

while(iter.hasNext() ) {
ct = (ClassType) iter.next();
System.out.println("Marshalling a class: ");
marshaller.marshal( ct , System.out );
}

------------------------------------ The docuemnt sample file

<?xml version="1.0" encoding="UTF-8"?>
<ontech:package xmlns:ontech="http://www.on.com/xml/schemas"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<ontech:classes>
<ontech:class name="test"/>
<ontech:class name="sample" description="A class description goes here."/>
</ontech:classes>

<ontech:comment>
Installs application Sample.
You will require Application Ver 2.4 CD to populate the depot and make this
package usable.
</ontech:comment>

</ontech:package>


----------------------------------- Schema file


<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.on.com/xml/schemas"
xmlns="http://www.on.com/xml/schemas"
elementFormDefault="unqualified"
attributeFormDefault="unqualified">
<xsd:element name="package" type="PackageType"/>

<xsd:element name="comment" type="TextType"/>

<xsd:element name="classes" type="ClassesType">
<xsd:unique name="ClassKey">
<xsd:selector xpath=".//class"/>
<xsd:field xpath="@name"/>
</xsd:unique>
</xsd:element>

<xsd:complexType name="PackageType">
<!-- Sequenced entities -->
<xsd:all>

<!-- <xsd:element name="info" type="InfoType"/> -->
<!-- <xsd:element name="info" type="InfoType2"/> -->

<xsd:element ref="comment" minOccurs="0"/>

<xsd:element ref="classes" minOccurs="0"/>
</xsd:complexType>

<xsd:element name="class">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="description" type="xsd:string" use="optional"/>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="ClassesType">
<xsd:sequence>
<xsd:element ref="class" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="TextType">
<xsd:complexContent mixed="true">
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<!-- <xsd:attribute name="name" type="ScriptNameType"/> -->
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>


<jxb:bindings version="1.0"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema">

<jxb:bindings schemaLocation="package.xsd" node="/xs:schema">
<!-- Illustrate binding model group to content interfaces. -->

<jxb:globalBindings bindingStyle="modelGroupBinding"/>

<!-- Systematically ensure that symbol space collisions
between the element symbol space and other symbol
spaces are resolved by appending "Element" to
all element interfaces. This customization
resolves name collision between element FooBar
and complexType FooBar. The element interface
for element <FooBar> is FooBarElement because of
this customization. It will also correct all other
collisions between element and type definition namespaces.
-->

--------------------------- Bindings file :


<jxb:schemaBindings>
<jxb:package name="com.on.ccm.devstudio.data.jaxb"/>
<jxb:nameXmlTransform>
<jxb:elementName suffix="Element"/>
</jxb:nameXmlTransform>
</jxb:schemaBindings>

<!-- Resolve collision between XML name and Java keyword "class". -->
<jxb:bindings node="//xs:element[@name='class']">
<!-- Customize Element interface name -->
<jxb:class name="Clazz"/>
<!-- All references to this global element will be by this
customized property name. -->
<jxb:property name="Clazz"/>
</jxb:bindings>

</jxb:bindings>
</jxb:bindings>