XML Object Binder
________ Current version: 3.1 Hosted by SourceForge.net Logo
Menu
Main page
Quick Example
Userguide [PDF]
Download
Tracker
CVS

Examples
Example 1

Parsing XML file to Object example
Take the following XML file:
    <purchaseOrder shipped="false">
        <orderDate>2003-04-22</orderDate>
        <productId>832684</productId>
        <productId>346734</productId>
        <customerId>2674346</customerId>
        <comment>Delayed delivery</comment>
    </purchaseOrder>
    

Then you create the following interfaces:

    public interface PurchaseOrder extends XMLObject {
        public boolean getShipped();
        public void setShipped(boolean shipped);
        public OrderDate getOrderDate();
        public void setOrderDate(OrderDate orderDate);
        public void setShipped(boolean shipped);
        public Iterator getProductIds();
        public void addProductId(ProductId id);
        public void addProductIdGrouped(ProductId id);
        public CustomerId getCustomerId();
        public void setCustomerId(CustomerId id);
        public Comment getComment();
        public void setComment(Comment comment);
    }

    public interface OrderDate extends XMLObject {
        public xob.xml.datatypes.XSDDate getOrderDateValue();
        public void setOrderDateValue(xob.xml.datatypes.XSDDate orderDateValue);
    }

    public interface ProductId extends XMLObject {
        public int getProductIdValue();
        public void setProductIdValue(int value);
    }

    public interface CustomerId extends XMLObject {
        public int getCustomerIdValue();
        public void setCustomerIdValue(int value);
    }

    public interface Comment extends XMLObject {
        public String getCommentValue();
        public void setCommentValue(String value);
    }
    

Then this example code can be used to parse the XML file:

    import java.io.FileInputStream;
    import xob.XOBParseException;
    import xob.Factory;
    import xob.XMLObjectBinder;
    import xob.XMLUnmarshaller;

    public class PurchaseOrderReaderExample {

        public static void main(String[] args) throws Exception {
         XMLObjectBinder binder = Factory.createXMLObjectBinder(PurchaseOrder.class);
         XMLUnmarshaller unmarshaller = binder.createUnmarshaller();
         unmarshaller.setValidating(false); // We dont have a schema or DTD!

         try {
             PurchaseOrder po = (PurchaseOrder)unmarshaller.unmarshal(new FileInputStream(args[0]));

             System.out.println("Has been shipped:" + po.getShipped());
             System.out.println("Order date: " + po.getOrderDate().getOrderDateValue());
             for (Iterator it = po.getProductIds(); it.hasNext();) {
                ProductId prodId = (ProductId)it.next();
                System.out.println("ProductId:" + prodId.getProductIdValue());
             }
             System.out.println("CustomerId:" + po.getCustomerId().getCustomerIdValue());
             System.out.println("Comment:" + po.getComment().getCommentValue());     	
         }
         catch (XOBParseException xpe) {
             System.out.println("Failed to parse " + args[0] + " (" + xpe.getMessage() + ")");
         }                  
    }