How to persist entity classes in XML file

I described how to persists your objects in database using Toplink in previous prost. However, sometimes you might want to save your objects in XML. It's very simple with NetBeans 5.5 since we included JAXB library in IDE. Let's to create application that stores information about orders in xml instead of in DB. This approach might be used when you want to send your objects via network. Just create new project in Netbeans 5.5 and select Libraries node in project tab, righ-click and choose Add Library item and select JAXB 2.0 in list, don't forget to click OK button.
Now, create Item class that holds info related to one item. This class might have name and amount fields and appropriate set/get methods for them. Since, I would like to have XML schema type with itemOrder name in generated schema then put following annotation above class definition:
    @XmlType(name="orderItem")
    public class Item implements Serializable {
    ........
  
Order class is similar as Item. Order has reference to items and also order number and due date. This class is top level class and should be annotated with the @XmlRootElement annotation. See code snapshot:
   @XmlRootElement()
    public class Order implements Serializable {    
    
    private String orderNumber;
    private Calendar dueDate;
    private Collection  items;
 
Working with these objects is very simple and intuitive. I will show only storing objects in XML but creation of objects from XML or update them is similar. See storing order in XML file:
   JAXBContext ctx = JAXBContext.newInstance(Order.class);
        Marshaller m = ctx.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);       
        File xml = new File("order.xml");
        
        Order order = new Order();
        order.setOrderNumber("FSX-2345-5454");
        order.addItem(new Item("book", 1));
        order.addItem(new Item("pen", 1));
        
        m.marshal(order, new FileOutputStream(xml));
 
When you run application, you can check order.xml. However, what's about creation schema from classes, it's easy as well. Only add following target in build.xml and rebuild project.
   <target name="-pre-compile">
        <taskdef name="schemagen"
        classname="com.sun.tools.jxc.SchemaGenTask"
        classpath="${javac.classpath}"/>
        <echo message= "Generating schemas..." />
        <mkdir dir="${build.dir}/schemas" />
        <schemagen destdir="${build.dir}/schemas" classpath="${javac.classpath}">
            <src path="${src.dir}" />
            <schema file="order.xsd"/>
        </schemagen>
    </target>
 
Working with JAXB is very easy in Netbeans 5.5. You don't need to download JWSDP pack since JAXB is bundled in IDE. In this post only java to XML binding is presented but you can simple create XML to Java as well. It means you will create objects from existing schema.
Comments:

Post a Comment:
Comments are closed for this entry.
About

pblaha

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks