Not sure, Matthew. Let me have a more detailed look, and come back to you.

Werner

Durst, Matthew (Mission Systems) wrote:
I've recently started using Castor on a project and it has worked great
for everything I've needed it to do except for one piece. I'm hoping
someone on this list can shed some light on what I'm doing wrong. I
created a small example that will hopefully illustrate my problem.

What I'm trying to accomplish is that in my objects I have some database
fields that are codes and I have another field that contains a
description of that field. For example lets say I have a field for a
unit of measure with a value of "M". The description for this field
would be "meters". So I would want the xml to look something like
<fieldname description="meters">M</fieldname>. I can get this to work
once, but if I have more then one field in my object with a description
it will only work for the first one and then doesn't work for the rest.
Here is my example:

FieldTest.java:

public class FieldTest {
private String field1 = null;
    private String field1Description = null;
    private String field2 = null;
    private String field2Description = null;
public FieldTest() {} public String getField1() {
        return field1;
    }
public String getField1Description() {
        return field1Description;
    }
public void setField1(String value) {
        field1 = value;
    }
public void setField1Description(String value) {
        field1Description = value;
    }

    public String getField2() {
        return field2;
    }
public String getField2Description() {
        return field2Description;
    }
public void setField2(String value) {
        field2 = value;
    }
public void setField2Description(String value) {
        field2Description = value;
    }
}

Mapping.xml:

<?xml version="1.0"?>
<mapping>
  <class name="FieldTest">
      <field name="field1" type="string">
         <bind-xml/>
      </field>
      <field name="field2" type="string">
         <bind-xml/>
      </field>
      <field name="field1Description" type="string">
         <bind-xml location="field1" node="attribute"/>
      </field>
      <field name="field2Description" type="string">
         <bind-xml location="field2" node="attribute"/>
      </field>
  </class>
</mapping>

CastorTest.java

public class CastorTest {

    public static void main(String args[]) {

        FileWriter writer = null;
        try {
            writer = new FileWriter("output.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            Mapping mapping = new Mapping();
            mapping.loadMapping("mapping.xml");

            FieldTest fieldTest = new FieldTest();
            fieldTest.setField1("field1 value");
            fieldTest.setField1Description("field1 description");
            fieldTest.setField2("field2 value");
            fieldTest.setField2Description("field2 description");
            Marshaller marshaller = new Marshaller(writer);
            marshaller.setSuppressXSIType(true);
            marshaller.setMapping(mapping);
            marshaller.marshal(fieldTest);
        } catch (MarshalException e) {
            e.printStackTrace();
        } catch (ValidationException e) {
            e.printStackTrace();
        } catch (MappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output.xml:

<?xml version="1.0" encoding="UTF-8" ?> <field-test> <field1 field1-description="field1 description">field1 value</field1> <field2>field2 value</field2> </field-test>


As you can see the field1Description is mapped to the proper location.
However, the field2Description is completely lost and doesn't show up
anywhere in the xml. If I delete the mapping for field1Description from
the mapping file then field2Description is displayed in the proper
location. Am I miss using the location attribute or is there something
I'm missing?

Matt Durst

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