Do not use on any
shared computer
August 7, 2008 04:55pm pdt
 
[x]
Attachment Details

Castor XML Mapping not working properly

Tags: castor, mapping, xml
hi,
     I am using Castor to convert java object to XML file. I customize the castor using mapping file, there i changed the class name and other field name but in the ouput xml file is not change.

 java class


package test;

public class ClientData {
      
          private String _name;
          private String _address;
        private int age;
       
       
          public int getAge() {
                  return age;
            }

            public void setAge(int age) {
                  this.age = age;
            }

            public void setName(String name) {
              _name = name;
          }

          public String getName() {
              return _name;
          }

          public void setAddress(String address) {
              _address = address;
          }

          public String getAddress() {
              return _address;
          }
}


Mapping file:
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
                         "http://castor.org/mapping.dtd">

<mapping>
  <class name="test.ClientData">
  <map-to xml="ClientData"/>
    <field name="Name"
           type="java.lang.String">
      <bind-xml name="Name" node="element"/>
    </field>

    <field name="Address"
           type="java.lang.String">
      <bind-xml name="new-Address" node="element"/>
    </field>
   
    <field name="Age"
           type="java.lang.Integer">
      <bind-xml name="ages" node="element"/>
    </field>
  </class>
</mapping>


Main file:

public class Test {


      public static void main(String[] args) throws IOException, MappingException, MarshalException, ValidationException {
      
ClientData o = new ClientData();
            o.setAddress("1");
            o.setName("ss");
            o.setAge(3);

               FileWriter writer = new FileWriter("test.xml");
                    Marshaller marshaller = new Marshaller(writer);
                    Mapping mapping = new Mapping();
                    mapping.loadMapping(new URL("file:///D:/workspace/CastorSample/src/map.xml"));
                    marshaller.setMapping(mapping);
                    marshaller.setEncoding("iso-8859-1");
                    marshaller.marshal(o,writer);
                    writer.close();
}

      
}


Then the generated example file "test.xml"

<?xml version="1.0" encoding="UTF-8"?>
<client-data age="3">
<address>1</address>
<name>ss</name>
</client-data>

in the mapping file i given the map-to xml as "ClientData" but still its like <client-data>
in the mapping file i given all the fields as "element" but age is still coming as attribute.

I dont know why this is not working, the generated output file is like the default file generated by castor without using any mapping file.

Send me u r reply soon
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: deeppra
Question Asked On: 10.30.2007
Participating Experts: 1
Points: 100
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by objects

Rank: Genius

Expert Comment by objects:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by deeppra
Author Comment by deeppra:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by deeppra
Author Comment by deeppra:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by objects

Rank: Genius

Expert Comment by objects:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by deeppra
Author Comment by deeppra:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628