hi werner,
thanks again for your quick reply, and sorry for not sending an example - i was
quite sure there was a simple way of configuration using a mapping file, which
i have overseen.
to your questions:
-why not generate code from xsd?
i am working with wsdl, which contains more than one schema section. elements
defined there are crosslinked via (target)namespaces. honestly, i am not very
pleased about extracting these schemas to generate code for each. i prefer the
'manual way' of mapping my objects. i have hoped this way would be more robust
to changes as well.
-is there an example?
abstracting the real situation is a little bit tricky, but here we go:
public class A {
private String name;
private Map<String, B<String>> strings = new HashMap<String, B<String>>(3);
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void addString(B<String> s) {
this.strings.put(s.getName(), s);
}
public Map<String, B<String>> getStrings() {
return this.strings;
}
}
public B <T> {
private T value;
private String name;
public void setValue(T value) {
this.value = value;
}
public T getValue() {
return this.value;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
<mapping>
<!-- this mapping has not yet been pimped to qualify attributes -->
<class name="com.xyz.A">
<map-to xml="OBJECT"
ns-uri="http://xyz.com"
ns-prefix="p"/>
<field name="name" type="string">
<bind-xml name="objectName" node="attribute"/>
</field>
<field name="strings"
collection="map" type="com.xyz.B"
set-method="addString" get-method="getStrings">
<bind-xml name="STRING" node="element"/>
</field>
</class>
<class name="org.exolab.castor.mapping.MapItem">
<field name="key" type="string" transient="true"/>
<field name="value" type="com.xyz.B" container="true"/>
</class>
<class name="com.xyz.B">
<map-to ns-uri="http://xyz.com"
ns-prefix="p"/>
<field name="name" type="string">
<bind-xml node="attribute"/>
</field>
<field name="value" type="other">
<bind-xml node="text"/>
</field>
</class>
</mapping>
the test
public void testMarshalUnmarshal() {
B<String> wordsOfWisdom = new B<String>();
wordsOfWisdom.setValue("Hello World");
wordsOfWisdom.setName("wordsOfWisdom");
A myObject = new A();
myObject.setName("myObject");
myObject.addString(wordsOfWisdom);
Mapping mapping = new Mapping();
mapping.loadMapping("<location-of-mapping-file>");
StringWriter writer = new StringWriter();
marshaller.setWriter(writer);
marshaller.setMapping(mapping);
marshaller.marshal(myObject);
String actual = writer.toString();
if (logger.isDebugEnabled()) logger.debug(actual);
Reader reader = new StringReader(actual);
unmarshaller.setMapping(mapping);
Object o = unmarshaller.unmarshal(reader);
assertTrue(o instanceof A);
assertEquals(myObject.getName(), ((A) o).getName());
assertNotNull(((A) o).getStrings());
assertEquals(1, ((A) o).getStrings().size());
B<String> _wordsOfWisdom = ((A) o).getStrings().get(wordsOfWisdom.getName());
assertNotNull(_wordsOfWisdom);
assertEquals(wordsOfWisdom.getName(), _wordsOfWisdom.getName());
assertEquals(wordsOfWisdom.getValue(), _wordsOfWisdom.getValue());
}
actual marshalled example output:
<?xml version="1.0" encoding="UTF-8"?>
<p:OBJECT xmlns:p="http://xyz.com" objectName="myObject">
<p:STRING name="wordsOfWisdom">Hello World</p:STRING>
</p:OBJECT>
desired output
<?xml version="1.0" encoding="UTF-8"?>
<!-- attributes should be qualified -->
<p:OBJECT xmlns:p="http://xyz.com" p:objectName="myObject">
<p:STRING p:name="wordsOfWisdom">Hello World</p:STRING>
</p:OBJECT>
following your suggestion, i added an 'xmlns:p="http://xyz.com"' attribute on
each <class> element level (which is not valid according to DTD) and fixed the
<bind-xml> 'name' attribute. when i run my test (which is succesful with
unqualified attributes) it fails with an SAXException complaining
"unable to find FieldDescriptor for 'STRING' in ClassDescriptor of OBJECT{...}
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:732)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:588)"
hmm, i am not sure what to do next. do you have any suggestions once more?
thx, reinhard!
-----Ursprüngliche Nachricht-----
Von: Werner Guttmann [mailto:[EMAIL PROTECTED]
Gesendet: Mo 16.06.2008 20:22
An: [email protected]
Betreff: Re: [castor-user] how to qualify marshalled attributes?
Actually, have a look at the following mapping for a very simple Entity
class:
<?xml version="1.0" ?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<mapping>
<class name="xml.reinhard.Entity" xmlns:r="http://test">
<map-to xml="entity" ns-prefix="r" ns-uri="http://test"/>
<field name="id" type="integer" >
<bind-xml name="id" />
</field>
<field name="name" type="string">
<bind-xml name="name" />
</field>
<field name="att" type="string">
<bind-xml name="r:att" node="attribute" />
</field>
</class>
</mapping>
This works out of the box. Please *note* that removing the xmlns:r
namespace declaration will cause a failure when unmarshalling the
mapping file.
I hope this helps.
Werner
Werner Guttmann wrote:
> Reinhard,
>
> please read inline.
>
> Reinhard Weiss wrote:
>> hi,
>>
>> is there a way to force qualification of attributes in the marshalled
>> xml output (e.g. in case the schema defines
>> attributeFormDefault="qualified")?
> If you happen to have an XML schema at hands (and you last comment seems
> to indicate this), why don't you generate domain classes and descriptor
> classes from the XML schema, and things will be taken care of
> automatically ?
>
>> qualification of elements can easily be done via the <map-to>
>> element. but i have not found a way to define namespaces for
>> attributes.
> Can I assume that you are using a mapping file ?
>> eg using the 'QName-prefix' in the <bind-xml> does not
>> seem to have much effect.
> Have you tried to use a <bind-xml> element such as
>
> <bind-xml name="a:attribute" />
>
> I know this might not feel right, but let's try to narrow this down
> first ? If you had a *minimal * test case, please feel free to make it
> available ? The less time I spend on setting things up, the more I cam
> e.g. debug source code to see what's going wrong (in case this is a bug)
> or what's missing configuration-wise.
>
>> thx,
>>
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>>
>> DI Reinhard Weiss Java Developer
>>
>> ANECON Software Design und Beratung G.m.b.H. Alser Straße 4 / Hof 1
>> A-1090 WIEN
>>
>> Tel.: +43 1 409 58 90-0 Fax: +43 1 409 58 90-998 Web:
>> http://www.anecon.com
>>
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>>
>> Zusätzliche Pflichtangaben von Kapitalgesellschaften gem. § 14 UGB:
>> FN166941b | Handelsgericht Wien | Firmensitz Wien
>>
>>
>> ---------------------------------------------------------------------
>> 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
<<winmail.dat>>
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email

