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]
-------------------------------------------------

Reply via email to