技术开发 频道

使用JAXB从一个对象模型中产生XML文档


IT168技术文档
    也可以注释具体的类Company和Individual来定制映射。首先作为XML文档的根元素,不得不使用@XmlRootElement注释来指定XML命名空间http://www.watermelon.example/customer。该例子中使用@XmlType.propOrder来定制属性。可以使用从超类Customer中继承像id,email,telephone,homeAddress等。

Annotated Company Class

 

 

 

 

Annotated Individual Class

 

 

 

 

@XmlRootElement(name = "company", namespace=

"http://www.watermelon.example/customer")
@XmlType(propOrder = {"id", "name", "contactName",
        "telephone", "email", "numberOfEmployees",
        "homeAddress", "deliveryAddresses"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Company extends Customer {

  @XmlAttribute
  private String name;
  private String contactName;
  private Integer numberOfEmployees;
  // Constructors, getters, setters
}

 

 

 

 

@XmlRootElement(name = "individual", namespace = 
"http://www.watermelon.example/customer")
@XmlType(propOrder = {"id", "lastname",
         "firstname", "dateOfBirth", "telephone",
         "email", "homeAddress",
         "deliveryAddresses"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Individual extends Customer {

  private String firstname;
  @XmlAttribute
  private String lastname;
  @XmlJavaTypeAdapter(DateAdapter.class)
  private Date dateOfBirth;
  // Constructors, getters, setters
}

 

 

 

 

      JAXB映射java.util.Date属性为默认值,例如,个体的出生日期将显示为下列格式:<dateOfBirth>1940-08-07T00:00:00.781+02:00</dateOfBirth> 

      为了格式化日期(如:07/08/1953),有两种选择: 
      1. 使用日期类型javax.xml.datatype.XMLGregorianCalendar而不使用java.util.Date。 
      2. 使用一个适配器,就像上面代码看到的那样,个体类Individual使用@XmlJavaTypeAdapter注释。@XmlJavaTypeAdapter(DateAdapter.class)通知JAXB使用适配器调用DateAdapter当marshalling/unmarshalling属性dateOfBirth时。 

      写一个类(DateAdapter)来继承XmlAdapter。覆盖marshal 和 unmarshal方法。这种方法可以将日期按照一定格式的字符串进行格式化,反之亦然。下列代码使用java.text.SimpleDateFormat来格式化日期:
public class DateAdapter extends XmlAdapter<String, Date> { DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); public Date unmarshal(String date) throws Exception { return df.parse(date); } public String marshal(Date date) throws Exception { return df.format(date); } }
      现在返回Listing 1,如果Marshaller.marshal()方法被调用,DateAdapter.marshal()也被调用,出生日期也被格式化了.下面是获得的XML文档:

Individual XML Document

 

 

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:individual lastname="Starr" id="1" xmlns:ns2="http://www.watermelon.example/customer">
    <firstname>Ringo</firstname>
    <dateOfBirth>07/08/1940</dateOfBirth>
    <telephone>+187445</telephone>
    <email>ringo@star.co.uk</email>
    <homeAddress>
        <street>Abbey Road</street>
        <zip>SW14</zip>
        <city>London</city>
        <country>UK</country>
    </homeAddress>
    <delivery>
        <address>
            <street>Findsbury Avenue</street>
            <zip>CE451</zip>
            <city>London</city>
            <country>UK</country>
        </address>
        <address>
            <street>Camden Street</street>
            <zip>NW487</zip>
            <city>Brighton</city>
            <country>UK</country>
        </address>
    </delivery>
</ns2:individual>

 

 

 

 

Company XML Document

 

 

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:company name="Sony" id="1" xmlns:ns2="http://www.watermelon.example/customer">
    <contactName>Mr Father</contactName>
    <telephone>+14519454</telephone>
    <email>contact@sony.com</email>
    <numberOfEmployees>25000</numberOfEmployees>
    <homeAddress>
        <street>General Alley</street>
        <zip>75011</zip>
        <city>Paris</city>
        <country>FR</country>
    </homeAddress>
    <delivery>
        <address>
            <street>St James St</street>
            <zip>SW14</zip>
            <city>London</city>
            <country>UK</country>
        </address>
        <address>
            <street>Central Side Park</street>
            <zip>7845</zip>
            <city>New York</city>
            <country>US</country>
        </address>
    </delivery>
</ns2:company>

 

 

 

 


0
相关文章