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/
CastorSamp
le/src/map
.xml"));
marshaller.setMapping(mapp
ing);
marshaller.setEncoding("is
o-8859-1")
;
marshaller.marshal(o,write
r);
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