Thank you for your solution, it does work :)
The purpose that I use such an ugly format is because the original xml is
generated line by line manfully and are not well formatted.
To make it easy I made those two arguments to String, but actually the class
Bar is:
public class Bar {
public String name;
public ArrayList value;
public String getName(){
return name;
}
public void setName(String a){
name = a;
}
public ArrayList getValue(){
return value;
}
public void setValue(ArrayList a){
value = a;
}
}
and the xml is:
<?xml version="1.0" encoding="UTF-8"?>
<Foo>
<name name="bbb"/>
<value value="bbb"/>
<value value="ccc"/>
</Foo>
Following your solution I write the mapping like this:
<mapping>
<class name="Foo">
<map-to xml="Foo"/>
<field name="bar.name" type="string">
<bind-xml name="name" location="name" node="attribute"/>
</field>
<field name="bar.value" collection="arraylist">
<bind-xml name="value" location="value" node="attribute"/>
</field>
</class>
</mapping>
But I got an exception:
java.lang.NullPointerException
at java.util.Hashtable.get(Hashtable.java:333)
at
org.exolab.castor.mapping.loader.MappingLoader.getDescriptor(MappingLoader.java:209)
at
org.exolab.castor.xml.XMLMappingLoader.resolveRelations(XMLMappingLoader.java:180)
at
org.exolab.castor.mapping.loader.MappingLoader.loadMapping(MappingLoader.java:331)
at org.exolab.castor.mapping.Mapping.getResolver(Mapping.java:292)
at org.exolab.castor.mapping.Mapping.getResolver(Mapping.java:247)
at org.exolab.castor.xml.Unmarshaller.setMapping(Unmarshaller.java:475)
-----Original Message-----
From: Gregory Block [mailto:[EMAIL PROTECTED]
Sent: 2005年8月2日 星期二 19:10
To: [email protected]
Subject: Re: [castor-user] question about castor
1) Try something like:
<class name="Foo">
<map-to xml="Foo"/>
<field name="bar.name" type="string">
<bind-xml name="name" location="name" node="attribute"/>
</field>
<field name="bar.value" type="string">
<bind-xml name="value" location="val" node="attribute"/>
</field>
</class>
Where the location attribute specifies the inner node, and the
attribute itself is mapped to that location. Note that the field
names assume accessor methods that match its default naming; there's
no way to use get-method/set-method with that accessor structure,
unfortunately - it will assume getBar(), and will need it. Also note
that setBar is never going to get called, methinks - it assumes that
bar will always be a component, so you'll probably have to deal with
providing a default bar - it doesn't know anything about bar, the
class; only that it should use accessor methods to get at a field by
making a call to getBar() before calling setValue().
Assuming that you're using standard bean naming, and that location
supports this kind of construction, it should work. Having said
that, it's dead ugly - you're better off with
<Foo>
<name>xxx</name>
<value>xxx</value>
</Foo>
which is much easier to write, and doesn't rely on the location mess;
it also doesn't leave a bunch of nodes around whose sole purpose is
basically to hold a value with the same name as the node, and also
means that you could use attributes on the name or value node to
describe the contents of the name/value node (such as the language,
etc.) A mapping for that might look like
<class name="Foo">
<map-to xml="Foo"/>
<field name="bar.name" type="string">
<bind-xml name="name" />
</field>
<field name="bar.value" type="string">
<bind-xml name="value"/>
</field>
</class>
However, assuming that you don't need/want subnodes at all - which is
the case only in situations where you do not wish to "describe" the
contents of the name/value node with attributes - I fail to see why
<Foo name="" value=""> doesn't do what you want; and that's just
<class name="Foo">
<map-to xml="Foo"/>
<field name="bar.name" type="string">
<bind-xml name="name" node="attribute"/>
</field>
<field name="bar.value" type="string">
<bind-xml name="value" node="attribute"/>
</field>
</class>
The use of method-walking in the name field, as well as the use of
location in bind-xml, have quirks; not all theoretical uses work in
practice, and it's sometimes difficult to get the behavior you expect
out of it. Relying on strange constructions in XML that don't match
your real data is often of questionable use; in some complex cases,
you may find it more appropriate to write an "adapter" object which
you can chuck over your object to prepare an API more amenable to the
style of output you intend to use it for, without requiring data
copying between heavyweight representations.
On 2 Aug 2005, at 08:16, Shuai Zhang wrote:
> Hi folks,
> I want to use marshal-n-unmarshal two classes
> public class Foo {
> private Bar bar;
> public Foo();
> public getBar() {
> return bar;
> }
> public void setBar(Bar bar) {
> this.bar = bar;
> }
> }
> public class Bar{
> String name;
> String value;
> }
>
> The xml format I want is like this:
> <Foo>
> <name name=""/>
> <val value=""/>
> </Foo>
> Instead of:
> <Foo>
> <Bar>
> <name name=""/>
> <val value=""/>
> </Bar>
> </Foo>
>
> How can I write the mapping file?
>
> Thanks for your help:)
>
> ZS
>
> -------------------------------------------------
> If you wish to unsubscribe from this list, please
> send an empty message to the following address:
>
> [EMAIL PROTECTED]
> -------------------------------------------------
>
>
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------