Created
July 25, 2012 15:04
-
-
Save madankumarpc/3176642 to your computer and use it in GitHub Desktop.
Item XML file for Castor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<item> | |
<name>Cappucino</name> | |
<description>Coffee brewed</description> | |
<price>20</price> | |
</item> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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