Skip to content

Instantly share code, notes, and snippets.

@madankumarpc
Created July 25, 2012 15:04
Show Gist options
  • Save madankumarpc/3176642 to your computer and use it in GitHub Desktop.
Save madankumarpc/3176642 to your computer and use it in GitHub Desktop.
Item XML file for Castor
package com.tester;
import java.io.FileNotFoundException;
import java.io.FileReader;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import com.bean.Item;
public class Demo {
public static void main(String[] args) {
try {
Item item=(Item) Unmarshaller.unmarshal(Item.class, new FileReader("item.xml"));
System.out.println(item.getName());
System.out.println(item.getDescription());
System.out.println(item.getPrice());
} catch (MarshalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ValidationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.tester;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.xml.sax.InputSource;
import com.bean.Item;
public class Demo1 {
public Mapping retMapping() throws IOException, MappingException{
Mapping mapping=new Mapping();
mapping.loadMapping("mapping.xml");
return mapping;
}
public static void main(String[] args) {
try {
Demo1 d=new Demo1();
Mapping mapping=d.retMapping();
Unmarshaller unmarshaller=new Unmarshaller(Item.class);
unmarshaller.setMapping(mapping);
Item item=(Item) unmarshaller.unmarshal(new FileReader("item.xml"));
System.out.println(item.getName());
System.out.println(item.getDescription());
System.out.println(item.getPrice());
} catch (MarshalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ValidationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.bean;
public class Item {
private String description;
private String name;
private double price;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<item>
<name>Cappucino</name>
<description>Coffee brewed</description>
<price>20</price>
</item>
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
<description> Mapping example for the Item class</description>
<class name="com.bean.Item">
<field name="name" type="string">
<bind-xml name="ItemName" />
</field>
<field name="description" type="string"/>
<field name="price" type="double"/>
</class>
</mapping>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment