Skip to content

Instantly share code, notes, and snippets.

@madankumarpc
Created July 26, 2012 05:07
Show Gist options
  • Save madankumarpc/3180366 to your computer and use it in GitHub Desktop.
Save madankumarpc/3180366 to your computer and use it in GitHub Desktop.
Marshalling XML with Castor
<?xml version="1.0" encoding="UTF-8"?>
<invoice>
<item>
<name>Cappucino</name>
<description>Coffee brewed</description>
<price>20</price>
</item>
<item>
<name>Coffee Mocha</name>
<description>Coffee plus chocolate</description>
<price>20</price>
</item>
<item>
<name>Garlic bread</name>
<description>Wheat treat</description>
<price>30</price>
</item>
<item>
<name>Black coffee</name>
<description>True coffee without milk</description>
<price>25</price>
</item>
</invoice>
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.ValidationException;
import com.bean.Item;
public class MarshalDemo1 {
public static void main(String[] args) {
Item item=new Item();
item.setName("Coffee Mocha");
item.setDescription("Coffee and chocolate");
item.setPrice(30);
FileWriter fileWriter;
try {
Mapping mapping=new Mapping();
mapping.loadMapping("mapping.xml");
fileWriter = new FileWriter("item_updated.xml");
Marshaller marshaller=new Marshaller(fileWriter);
marshaller.setMapping(mapping);
marshaller.marshal(item);
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (MarshalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ValidationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.tester;
import java.io.FileWriter;
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.Marshaller;
import org.exolab.castor.xml.ValidationException;
import com.bean.Invoice;
import com.bean.Item;
public class MarshalDemo {
public static void main(String[] args) {
Item item1=new Item();
item1.setName("Coffee Mocha");
item1.setDescription("Coffee and chocolate");
item1.setPrice(30);
Item item2=new Item();
item2.setName("Cappucino");
item2.setDescription("Coffee brewed");
item2.setPrice(20);
Item item3=new Item();
item3.setName("Garlic bread");
item3.setDescription("Wheat treat");
item3.setPrice(30);
Item item4=new Item();
item4.setName("Black coffee");
item4.setDescription("True coffee with milk");
item4.setPrice(30);
Invoice invoice = new Invoice();
invoice.addItem(item1);
invoice.addItem(item2);
invoice.addItem(item3);
invoice.addItem(item4);
FileWriter fileWriter;
try {
Mapping mapping=new Mapping();
mapping.loadMapping("mapping.xml");
fileWriter = new FileWriter("invoice_updated.xml");
Marshaller marshaller=new Marshaller(fileWriter);
marshaller.setMapping(mapping);
marshaller.marshal(invoice);
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (MarshalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ValidationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.tester;
import java.io.FileWriter;
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.Marshaller;
import org.exolab.castor.xml.ValidationException;
import org.exolab.castor.xml.XMLContext;
import com.bean.Item;
public class MarshalXmlContext {
public static void main(String[] args) {
Item item=new Item();
item.setName("Coffee Mocha");
item.setDescription("Coffee and chocolate");
item.setPrice(30);
FileWriter fileWriter;
try {
XMLContext context=new XMLContext();
Mapping mapping=context.createMapping();
mapping.loadMapping("mapping.xml");
context.addMapping(mapping);
fileWriter = new FileWriter("item_updated.xml");
Marshaller marshaller=context.createMarshaller();
marshaller.setWriter(fileWriter);
marshaller.marshal(item);
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (MarshalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ValidationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.tester;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
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.Invoice;
import com.bean.Item;
public class Demo {
public Mapping retMapping() throws IOException, MappingException{
Mapping mapping=new Mapping();
mapping.loadMapping("mapping.xml");
return mapping;
}
public static void main(String[] args) {
try {
Demo d=new Demo();
Mapping mapping=d.retMapping();
Unmarshaller unmarshaller=new Unmarshaller(Invoice.class);
unmarshaller.setMapping(mapping);
Invoice invoice=(Invoice) unmarshaller.unmarshal(new FileReader("invoice.xml"));
Vector items=invoice.getItems();
for(int i=0;i<items.size();i++)
{
Item item=(Item)items.elementAt(i);
System.out.println(item.getName());
System.out.println(item.getDescription());
System.out.println(item.getPrice());
}
System.out.println(invoice.getTotal());
} 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.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.exolab.castor.xml.XMLContext;
import org.xml.sax.InputSource;
import com.bean.Item;
public class UnmarshallXmlContext {
public static void main(String[] args) {
try {
XMLContext context = new XMLContext();
Mapping mapping=context.createMapping();
mapping.loadMapping("mapping.xml");
context.addMapping(mapping);
Unmarshaller unmarshaller=context.createUnmarshaller();
unmarshaller.setClass(Item.class);
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment