学习dom4j的jaxb实现(半成品)

primer.xml

<?xml version="1.0"?>
<purchaseOrders>
  <!-- 1st -->
  <purchaseOrder id="1" orderDate="2004-06-28">
    <shipTo country="US">
      <name>Alice Smith</name>
      <street>123 Maple Street</street>
      <city>Cambridge</city>
      <state>MA</state>
      <zip>12345</zip>
    </shipTo>
    <billTo country="US">
      <name>Robert Smith</name>
      <street>8 Oak Avenue</street>
      <city>Cambridge</city>
      <state>MA</state>
      <zip>12345</zip>
    </billTo>
    <items/>
  </purchaseOrder>
  <!-- 2nd -->
  <purchaseOrder id="2" orderDate="2004-06-29">
    <shipTo country="US">
      <name>Lee Grant</name>
      <street>123 Maple Street</street>
      <city>Cambridge</city>
      <state>MA</state>
      <zip>12345</zip>
    </shipTo>
    <billTo country="US">
      <name>Stonewall  Jackson</name>
      <street>8 Oak Avenue</street>
      <city>Lexington</city>
      <state>MA</state>
      <zip>12345</zip>
    </billTo>
    <items/>
  </purchaseOrder>
</purchaseOrders>
 

 

Item.java

import java.io.Serializable;

import javax.xml.bind.Element;

public class Items implements Serializable, Element
{
    private static final long serialVersionUID = 6041417537367507061L;

}

 USAddress.java

import java.io.Serializable;
import java.math.BigDecimal;

import javax.xml.bind.Element;

public class USAddress implements Serializable, Element
{
    private static final long serialVersionUID = -590607725333909750L;
    private String            name;

    private String            street;

    private String            city;

    private String            state;

    private String            country;

    private BigDecimal        zip;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getStreet()
    {
        return street;
    }

    public void setStreet(String street)
    {
        this.street = street;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getState()
    {
        return state;
    }

    public void setState(String state)
    {
        this.state = state;
    }

    public BigDecimal getZip()
    {
        return zip;
    }

    public void setZip(BigDecimal zip)
    {
        this.zip = zip;
    }

    public String getCountry()
    {
        return country;
    }

    public void setCountry(String country)
    {
        this.country = country;
    }
}

 PurchaseOrder.java

import java.io.Serializable;
import java.util.Calendar;

import javax.xml.bind.Element;

/**
 * @author linzq
 * 
 */
public class PurchaseOrder implements Serializable, Element
{
    private static final long serialVersionUID = 487350120910629296L;

    private Integer           id;

    private Calendar          orderDate;

    private USAddress         billTo;

    private USAddress         shipTo;

    private Items             items;

    public Calendar getOrderDate()
    {
        return orderDate;
    }

    public void setOrderDate(Calendar orderDate)
    {
        this.orderDate = orderDate;
    }

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public USAddress getBillTo()
    {
        return billTo;
    }

    public void setBillTo(USAddress billTo)
    {
        this.billTo = billTo;
    }

    public USAddress getShipTo()
    {
        return shipTo;
    }

    public void setShipTo(USAddress shipTo)
    {
        this.shipTo = shipTo;
    }

    public Items getItems()
    {
        return items;
    }

    public void setItems(Items items)
    {
        this.items = items;
    }
}

 PurchaseOrders.java

import java.io.Serializable;
import java.util.List;

import javax.xml.bind.Element;

/**
 * @author linzq
 * 
 */
public class PurchaseOrders implements Serializable, Element
{
    private static final long serialVersionUID = -1845681258981901430L;

    List<PurchaseOrder>       purchaseOrder;

    public List<PurchaseOrder> getPurchaseOrder()
    {
        return purchaseOrder;
    }

    public void setPurchaseOrder(List<PurchaseOrder> purchaseOrder)
    {
        this.purchaseOrder = purchaseOrder;
    }

}

 ObjectFactory.java

public class ObjectFactory
{
    public static PurchaseOrders createPurchaseOrders()
    {
        return new PurchaseOrders();
    }

    public static PurchaseOrder createPurchaseOrder()
    {
        return new PurchaseOrder();
    }

    public static USAddress createUSAddress()
    {
        return new USAddress();
    }

    public static Items createItems()
    {
        return new Items();
    }
}

 PurchaseOrderHandler.java

public class PurchaseOrderHandler implements JAXBObjectHandler
{

    @Override
    public void handleObject(javax.xml.bind.Element jaxbElement)
            throws Exception
    {
        PurchaseOrder order = (PurchaseOrder) jaxbElement;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        System.out.println("Order - id:" + order.getId() + ", date:" +
                format.format(order.getOrderDate().getTime()));
    }
}

 PurchaseOrderDateModifier.java

public class PurchaseOrderDateModifier implements JAXBObjectModifier
{

    @Override
    public javax.xml.bind.Element modifyObject(
            javax.xml.bind.Element jaxbElement) throws Exception
    {
        PurchaseOrder order = (PurchaseOrder) jaxbElement;

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        order.setOrderDate(calendar);
        return order;
    }
}

 JAXBDemo.java

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Calendar;

import javax.xml.bind.JAXBException;

import org.dom4j.io.OutputFormat;
import org.dom4j.jaxb.JAXBWriter;
import org.xml.sax.SAXException;

/**
 * @author linzq
 * 
 */
public class JAXBDemo
{

    public static String getOutputDir()
    {
        return JAXBDemo.class.getResource(".").getFile().toString();
    }

    /**
     * @param args
     * @throws IOException
     * @throws SAXException
     * @throws JAXBException
     */
    public static void main(String[] args) throws IOException, SAXException,
            JAXBException
    {
        System.out
                .println(JAXBDemo.class.getResource(".").getFile().toString());
        System.exit(0);
        ObjectFactory factory = new ObjectFactory();

        PurchaseOrders orders = factory.createPurchaseOrders();

        // Order 1
        PurchaseOrder order = factory.createPurchaseOrder();

        USAddress billTo = factory.createUSAddress();
        billTo.setCity("Cambridge");
        billTo.setCountry("US");
        billTo.setName("Robert Smith");
        billTo.setState("MA");
        billTo.setStreet("8 Oak Avenue");
        billTo.setZip(new BigDecimal(12345));
        order.setBillTo(billTo);

        USAddress shipTo = factory.createUSAddress();
        shipTo.setCity("Cambridge");
        shipTo.setCountry("US");
        shipTo.setName("Alice Smith");
        shipTo.setState("MA");
        shipTo.setStreet("123 Maple Street");
        shipTo.setZip(new BigDecimal(12345));
        order.setShipTo(shipTo);

        Calendar orderDate = Calendar.getInstance();
        orderDate.set(2004, 06, 30);
        order.setOrderDate(orderDate);

        Items items = factory.createItems();
        order.setItems(items);

        orders.getPurchaseOrder().add(order);

        // Order 2
        PurchaseOrder order2 = factory.createPurchaseOrder();

        USAddress billTo2 = factory.createUSAddress();
        billTo2.setCity("Cambridge");
        billTo2.setCountry("US");
        billTo2.setName("Robert Smith");
        billTo2.setState("MA");
        billTo2.setStreet("8 Oak Avenue");
        billTo2.setZip(new BigDecimal(12345));
        order2.setBillTo(billTo2);

        USAddress shipTo2 = factory.createUSAddress();
        shipTo2.setCity("Cambridge");
        shipTo2.setCountry("US");
        shipTo2.setName("Alice Smith");
        shipTo2.setState("MA");
        shipTo2.setStreet("123 Maple Street");
        shipTo2.setZip(new BigDecimal(12345));
        order2.setShipTo(shipTo2);

        Calendar orderDate2 = Calendar.getInstance();
        orderDate2.set(2004, 06, 30);
        order2.setOrderDate(orderDate2);

        Items items2 = factory.createItems();
        order2.setItems(items2);

        orders.getPurchaseOrder().add(order2);

        File outputFile = new File(getOutputDir(), "jaxbWrite.xml");

        JAXBWriter jaxbWriter = new JAXBWriter("com.gbit.test.primer",
                OutputFormat.createPrettyPrint());
        jaxbWriter.setOutput(outputFile);

        jaxbWriter.startDocument();
        jaxbWriter.write(orders);
        jaxbWriter.endDocument();
    }
}

 编译和执行虽然没有错,但是没有预期的结果出现。现在就是在想,应该是在POJO需要实现javax.xml.bind.Marshaller的接口类似的方法。

 

 

 

 

你可能感兴趣的:(java,xml)