You'll need to create a Marshaller class and set the name of the root
element...

Marshaller m = new Marshaller(writer);
m.setRootElement("foo");
m.marshal(rootTypeName);

--Keith


Barnaby Morris wrote:
> 
> Thanks for the reply Arnaud.
> 
> Because I am have org.exolab.castor.builder.javaclassmapping=type in the
> castorbuilder.properties file, I only have classes generated for the complex
> types, not for the elements. Is it possible to specify to the complex type
> class instance what element name to use (perhaps on construction or with a
> set method)? This is only a problem for the root tag since all child
> elements' names are stored by the parent complex type classes. There is no
> parent complex type for the root, therefore no place to store the correct
> element name.
> 
> Thanks,
> 
> Barnaby
> 
> >From: "Arnaud Blandin" <[EMAIL PROTECTED]>
> >Reply-To: [EMAIL PROTECTED]
> >To: [EMAIL PROTECTED]
> >Subject: Re: [castor-dev] possible bug in code generation?
> >Date: Wed, 12 Jun 2002 08:08:31 +0200
> >
> >Hi Barnaby,
> >
> >You are getting the expected output.
> > >             RootTypeName t = new RootTypeName();
> > >             // display the marshalled version of this object
> > >             StringWriter sw = new StringWriter();
> > >             t.marshal(sw);
> >
> >you are marshalling RootTypeName, you should marshall RootElementName.
> >Castor won't decide for you which element to marshall.
> >
> >Imagine you have
> ><xsd:element name="foo" type="RootTypeName"/>
> ><xsd:element name="bar" type="RootTypeName"/>
> >which element do you want to see outputted when marshalling
> >'RootTypeName'?
> >
> >Castor provides all the necessary methods to do basically what you want
> >to do with the generated Object Model but it doesn't (and it won't)
> >prevent you from doing mistakes ;).
> >
> >Hope that helps,
> >
> >Arnaud
> >
> >
> > > -----Original Message-----
> > > From: Barnaby Morris [mailto:[EMAIL PROTECTED]]
> > > Sent: Tuesday, June 11, 2002 11:40 PM
> > > To: [EMAIL PROTECTED]
> > > Subject: [castor-dev] possible bug in code generation?
> > >
> > > I am wondering if I have found a bug or not.
> > >
> > > I am using the castor-0.9.3.19
> >org.exolab.castor.builder.SourceGenerator
> > > tool to generate Java sourcecode from a schema using the
> > > org.exolab.castor.builder.javaclassmapping=type setting in the
> > > castorbuilder.properties file. The xml generated from a marshaled
> >object
> > > isn't what I expect. Instead of the root element name specified in the
> > > schema, I get the ComplexType of the root element.
> > >
> > > I would appreciate it if somebody either confirmed that this is a bug
> >or
> > > told me that the existing behavior is correct.
> > >
> > > I have included an example of what I am talking about below. If it
> >would be
> > > helpful, I can send all of the source and compiled classes used to run
> >this.
> > >
> > > Thanks!
> > >
> > > Barnaby
> > >
> > > Here is an example:
> > >
> > > Using code generated from this schema (make sure to set
> > > org.exolab.castor.builder.javaclassmapping=type in the
> > > castorbuilder.properties file):
> > > =======================
> > > <?xml version="1.0"?>
> > > <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema";
> > > elementFormDefault="unqualified" attributeFormDefault="unqualified">
> > >     <xsd:element name="rootElementName" type="RootTypeName"/>
> > >     <xsd:complexType name="RootTypeName">
> > >             <xsd:sequence>
> > >                     <xsd:element name="innerSimpleElementName"
> > > type="xsd:string"/>
> > >                     <xsd:element name="innerComplexElementName"
> > > type="InnerTypeName"/>
> > >             </xsd:sequence>
> > >     </xsd:complexType>
> > >     <xsd:complexType name="InnerTypeName">
> > >             <xsd:sequence>
> > >                     <xsd:element name="b" type="xsd:string"/>
> > >             </xsd:sequence>
> > >     <xsd:attribute name="a" type="xsd:int"/>
> > >     </xsd:complexType>
> > > </xsd:schema>
> > > =======================
> > >
> > > Output I want:
> > > =======================
> > > <?xml version="1.0" encoding="UTF-8"?>
> > > <rootElementName>
> > >     <innerSimpleElementName>string 1</innerSimpleElementName>
> > >     <innerComplexElementName a="5">
> > >             <b>Hello</b>
> > >     </innerComplexElementName>
> > > </rootElementName>
> > > =======================
> > >
> > > Output I get (notice the 'RootTypeName' instead of 'rootElementName'):
> > > =======================
> > > <?xml version="1.0" encoding="UTF-8"?>
> > > <RootTypeName>
> > >     <innerSimpleElementName>string 1</innerSimpleElementName>
> > >     <innerComplexElementName a="5">
> > >             <b>Hello</b>
> > >     </innerComplexElementName>
> > > </RootTypeName>
> > > =======================
> > >
> > > This is the class I used to generate output:
> > > =======================
> > > import apackage.RootTypeName;
> > > import apackage.InnerTypeName;
> > > import java.io.StringWriter;
> > > import java.io.StringWriter;
> > >
> > > public class TestSimple {
> > >
> > >     public static void main(String[] args) {
> > >
> > >         try {
> > >             // create and populate an new instance
> > >             RootTypeName t = new RootTypeName();
> > >             t.setInnerSimpleElementName("string 1");
> > >
> > >             InnerTypeName it = new InnerTypeName();
> > >             t.setInnerComplexElementName(it);
> > >             it.setA(5);
> > >             it.setB("Hello");
> > >
> > >             // display the marshalled version of this object
> > >             StringWriter sw = new StringWriter();
> > >             t.marshal(sw);
> > >             System.out.println("xml output = '" + sw.toString() +
> >"'");
> > >
> > >
> > >         } catch (Exception x) {
> > >             x.printStackTrace();
> > >         }
> > >     }
> > > }
> > > =======================
> > >
> > >
> > >
> > > _________________________________________________________________
> > > Get your FREE download of MSN Explorer at
> >http://explorer.msn.com/intl.asp.
> > >
> > > -----------------------------------------------------------
> > > If you wish to unsubscribe from this mailing, send mail to
> > > [EMAIL PROTECTED] with a subject of:
> > >     unsubscribe castor-dev
> >
> >-----------------------------------------------------------
> >If you wish to unsubscribe from this mailing, send mail to
> >[EMAIL PROTECTED] with a subject of:
> >       unsubscribe castor-dev
> >
> 
> _________________________________________________________________
> Chat with friends online, try MSN Messenger: http://messenger.msn.com
> 
> -----------------------------------------------------------
> If you wish to unsubscribe from this mailing, send mail to
> [EMAIL PROTECTED] with a subject of:
>         unsubscribe castor-dev

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to