Hello,

using a code like:

StringWriter st = new StringWriter();
marshaller.marshal(object , st);
String xml = st.toString();

tells Castor to retrieve the mapping information (how Java structures
are mapped to XML structures) through reflection of the 'object'
instance.

If you want to influence the mapping e.g. by using a mapping file you
should use something like:

XXMLContext ctx = new XMLContext();
ctx.addClass(Xy.class); // or ctx.addMapping(loadedMapping);
Marshaller marshaller = ctx.createMarshaller();
StringWriter st = new StringWriter();
marshaller.setWriter(st);
marshaller.marshal(object);
String xml = st.toString();

the method you used is a static method that uses a new Marshaller
instance with default settings!

Hope this helps

Joachim

2008/10/21 Baba <[EMAIL PROTECTED]>:
> Hi Joshua,
>
> i use the same way to marshaling the java classes to string but in my
> case it's work.
>
> You are light on information about how you unmarshal your XML into
> java classes. My experiences with Castor told me in the most cases if
> the marshaling result is wrong it has its root in the way you
> unmarshal your XML. I use mapping files which define the attributes or
> elements and it works great.
>
> Greeting
> Thomas
>
> On Tue, Oct 21, 2008 at 3:49 PM, joshua1970 <[EMAIL PROTECTED]> wrote:
>>
>> Dear all,
>> I have looked at many examples about Marshalling but all examples do a
>> Marshalling
>> on a FileWriter stream. Is it possible to Marshal an XML directly to a
>> String ?
>> P.s. I have tried also with :
>>
>> StringWriter st = new StringWriter();
>> marshaller.marshal(object , st);
>>
>> String xml = st.toString();
>>
>> but then a strange thing happens....my attribute are turned into nodes!
>> f.e. instead of this:
>>
>> <node1 id="10" session="20">
>>
>> I get as result:
>>
>> <node1>
>>  <id>10</id>
>>  <session>20</session>
>> </node1>
>>
>> Any help !
>> joshua
>> --
>> View this message in context: 
>> http://www.nabble.com/How-to-Marshal-XML-to-a-String---tp20090707p20090707.html
>> Sent from the Castor - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to