[Java]XML与JAXB

转:http://philipho123.iteye.com/blog/1330632


a.xml 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <customer id="cust id">  
  3.     <address>  
  4.         <street>jinjihulu</street>  
  5.         <city>suzhou</city>  
  6.         <zip>215200</zip>  
  7.     </address>  
  8.     <name>Marshall</name>  
  9. </customer>  


a.xsd  
Xml代码   收藏代码
  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">   
  2.      
  3.     <xs:complexType name="Customer">   
  4.       <xs:sequence>   
  5.          <xs:element name="address" type="Address"/>   
  6.          <xs:element name="name" type="xs:string"/>   
  7.       </xs:sequence>   
  8.        <xs:attribute name="id" type="xs:string"/>         
  9.     </xs:complexType>   
  10.      
  11.       <xs:complexType name="Address">   
  12.       <xs:sequence>   
  13.          <xs:element name="street" type="xs:string"/>   
  14.          <xs:element name="city" type="xs:string"/>   
  15.          <xs:element name="zip" type="ZipCodeType"/>   
  16.        </xs:sequence>   
  17.    </xs:complexType>   
  18.     
  19.     <xs:simpleType name="ZipCodeType">   
  20.       <xs:restriction base="xs:integer">   
  21.          <xs:minInclusive value="10000"/>   
  22.          <xs:maxInclusive value="99999"/>   
  23.       </xs:restriction>   
  24.     </xs:simpleType>   
  25.     <xs:element name="customer" type="Customer"/>   
  26.     <xs:element name="address" type="Address"/>   
  27. </xs:schema>   



xjc生成java类  
D:\JavaSoft\jdk6\bin>xjc -d f:/xmldemo -p demo.xml f:/xmldemo/a.xsd 
parsing a schema... 
compiling a schema... 
demo\xml\Address.java 
demo\xml\Customer.java 
demo\xml\ObjectFactory.java 

Customer.java  
Java代码   收藏代码
  1. //  
  2. // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6   
  3. // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>   
  4. // Any modifications to this file will be lost upon recompilation of the source schema.   
  5. // Generated on: 2011.12.30 at 04:13:13 PM CST   
  6. //  
  7.   
  8.   
  9. package demo.xml;  
  10.   
  11. import javax.xml.bind.annotation.XmlAccessType;  
  12. import javax.xml.bind.annotation.XmlAccessorType;  
  13. import javax.xml.bind.annotation.XmlAttribute;  
  14. import javax.xml.bind.annotation.XmlElement;  
  15. import javax.xml.bind.annotation.XmlRootElement;  
  16. import javax.xml.bind.annotation.XmlType;  
  17.   
  18.   
  19. /** 
  20.  * <p>Java class for Customer complex type. 
  21.  *  
  22.  * <p>The following schema fragment specifies the expected content contained within this class. 
  23.  *  
  24.  * <pre> 
  25.  * &lt;complexType name="Customer"> 
  26.  *   &lt;complexContent> 
  27.  *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
  28.  *       &lt;sequence> 
  29.  *         &lt;element name="address" type="{}Address"/> 
  30.  *         &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/> 
  31.  *       &lt;/sequence> 
  32.  *       &lt;attribute name="id" type="{http://www.w3.org/2001/XMLSchema}string" /> 
  33.  *     &lt;/restriction> 
  34.  *   &lt;/complexContent> 
  35.  * &lt;/complexType> 
  36.  * </pre> 
  37.  *  
  38.  *  
  39.  */  
  40. @XmlRootElement  
  41. @XmlAccessorType(XmlAccessType.FIELD)  
  42. @XmlType(name = "Customer", propOrder = {  
  43.     "address",  
  44.     "name"  
  45. })  
  46. public class Customer {  
  47.   
  48.     @XmlElement(required = true)  
  49.     protected Address address;  
  50.     @XmlElement(required = true)  
  51.     protected String name;  
  52.     @XmlAttribute  
  53.     protected String id;  
  54.   
  55.     /** 
  56.      * Gets the value of the address property. 
  57.      *  
  58.      * @return 
  59.      *     possible object is 
  60.      *     {@link Address } 
  61.      *      
  62.      */  
  63.     public Address getAddress() {  
  64.         return address;  
  65.     }  
  66.   
  67.     /** 
  68.      * Sets the value of the address property. 
  69.      *  
  70.      * @param value 
  71.      *     allowed object is 
  72.      *     {@link Address } 
  73.      *      
  74.      */  
  75.     public void setAddress(Address value) {  
  76.         this.address = value;  
  77.     }  
  78.   
  79.     /** 
  80.      * Gets the value of the name property. 
  81.      *  
  82.      * @return 
  83.      *     possible object is 
  84.      *     {@link String } 
  85.      *      
  86.      */  
  87.     public String getName() {  
  88.         return name;  
  89.     }  
  90.   
  91.     /** 
  92.      * Sets the value of the name property. 
  93.      *  
  94.      * @param value 
  95.      *     allowed object is 
  96.      *     {@link String } 
  97.      *      
  98.      */  
  99.     public void setName(String value) {  
  100.         this.name = value;  
  101.     }  
  102.   
  103.     /** 
  104.      * Gets the value of the id property. 
  105.      *  
  106.      * @return 
  107.      *     possible object is 
  108.      *     {@link String } 
  109.      *      
  110.      */  
  111.     public String getId() {  
  112.         return id;  
  113.     }  
  114.   
  115.     /** 
  116.      * Sets the value of the id property. 
  117.      *  
  118.      * @param value 
  119.      *     allowed object is 
  120.      *     {@link String } 
  121.      *      
  122.      */  
  123.     public void setId(String value) {  
  124.         this.id = value;  
  125.     }  
  126.   
  127. }  



Address.java  
Java代码   收藏代码
  1. //  
  2. // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6   
  3. // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>   
  4. // Any modifications to this file will be lost upon recompilation of the source schema.   
  5. // Generated on: 2011.12.30 at 04:13:13 PM CST   
  6. //  
  7.   
  8.   
  9. package demo.xml;  
  10.   
  11. import javax.xml.bind.annotation.XmlAccessType;  
  12. import javax.xml.bind.annotation.XmlAccessorType;  
  13. import javax.xml.bind.annotation.XmlElement;  
  14. import javax.xml.bind.annotation.XmlType;  
  15.   
  16.   
  17. /** 
  18.  * <p>Java class for Address complex type. 
  19.  *  
  20.  * <p>The following schema fragment specifies the expected content contained within this class. 
  21.  *  
  22.  * <pre> 
  23.  * &lt;complexType name="Address"> 
  24.  *   &lt;complexContent> 
  25.  *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
  26.  *       &lt;sequence> 
  27.  *         &lt;element name="street" type="{http://www.w3.org/2001/XMLSchema}string"/> 
  28.  *         &lt;element name="city" type="{http://www.w3.org/2001/XMLSchema}string"/> 
  29.  *         &lt;element name="zip" type="{}ZipCodeType"/> 
  30.  *       &lt;/sequence> 
  31.  *     &lt;/restriction> 
  32.  *   &lt;/complexContent> 
  33.  * &lt;/complexType> 
  34.  * </pre> 
  35.  *  
  36.  *  
  37.  */  
  38. @XmlAccessorType(XmlAccessType.FIELD)  
  39. @XmlType(name = "Address", propOrder = {  
  40.     "street",  
  41.     "city",  
  42.     "zip"  
  43. })  
  44. public class Address {  
  45.   
  46.     @XmlElement(required = true)  
  47.     protected String street;  
  48.     @XmlElement(required = true)  
  49.     protected String city;  
  50.     protected int zip;  
  51.   
  52.     /** 
  53.      * Gets the value of the street property. 
  54.      *  
  55.      * @return 
  56.      *     possible object is 
  57.      *     {@link String } 
  58.      *      
  59.      */  
  60.     public String getStreet() {  
  61.         return street;  
  62.     }  
  63.   
  64.     /** 
  65.      * Sets the value of the street property. 
  66.      *  
  67.      * @param value 
  68.      *     allowed object is 
  69.      *     {@link String } 
  70.      *      
  71.      */  
  72.     public void setStreet(String value) {  
  73.         this.street = value;  
  74.     }  
  75.   
  76.     /** 
  77.      * Gets the value of the city property. 
  78.      *  
  79.      * @return 
  80.      *     possible object is 
  81.      *     {@link String } 
  82.      *      
  83.      */  
  84.     public String getCity() {  
  85.         return city;  
  86.     }  
  87.   
  88.     /** 
  89.      * Sets the value of the city property. 
  90.      *  
  91.      * @param value 
  92.      *     allowed object is 
  93.      *     {@link String } 
  94.      *      
  95.      */  
  96.     public void setCity(String value) {  
  97.         this.city = value;  
  98.     }  
  99.   
  100.     /** 
  101.      * Gets the value of the zip property. 
  102.      *  
  103.      */  
  104.     public int getZip() {  
  105.         return zip;  
  106.     }  
  107.   
  108.     /** 
  109.      * Sets the value of the zip property. 
  110.      *  
  111.      */  
  112.     public void setZip(int value) {  
  113.         this.zip = value;  
  114.     }  
  115.   
  116. }  



ObjectFactory.java  
Java代码   收藏代码
  1. //  
  2. // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6   
  3. // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>   
  4. // Any modifications to this file will be lost upon recompilation of the source schema.   
  5. // Generated on: 2011.12.30 at 04:13:13 PM CST   
  6. //  
  7.   
  8.   
  9. package demo.xml;  
  10.   
  11. import javax.xml.bind.JAXBElement;  
  12. import javax.xml.bind.annotation.XmlElementDecl;  
  13. import javax.xml.bind.annotation.XmlRegistry;  
  14. import javax.xml.namespace.QName;  
  15.   
  16.   
  17. /** 
  18.  * This object contains factory methods for each  
  19.  * Java content interface and Java element interface  
  20.  * generated in the generated package.  
  21.  * <p>An ObjectFactory allows you to programatically  
  22.  * construct new instances of the Java representation  
  23.  * for XML content. The Java representation of XML  
  24.  * content can consist of schema derived interfaces  
  25.  * and classes representing the binding of schema  
  26.  * type definitions, element declarations and model  
  27.  * groups.  Factory methods for each of these are  
  28.  * provided in this class. 
  29.  *  
  30.  */  
  31. @XmlRegistry  
  32. public class ObjectFactory {  
  33.   
  34.     private final static QName _Address_QNAME = new QName("""address");  
  35.     private final static QName _Customer_QNAME = new QName("""customer");  
  36.   
  37.     /** 
  38.      * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated 
  39.      *  
  40.      */  
  41.     public ObjectFactory() {  
  42.     }  
  43.   
  44.     /** 
  45.      * Create an instance of {@link Customer } 
  46.      *  
  47.      */  
  48.     public Customer createCustomer() {  
  49.         return new Customer();  
  50.     }  
  51.   
  52.     /** 
  53.      * Create an instance of {@link Address } 
  54.      *  
  55.      */  
  56.     public Address createAddress() {  
  57.         return new Address();  
  58.     }  
  59.   
  60.     /** 
  61.      * Create an instance of {@link JAXBElement }{@code <}{@link Address }{@code >}} 
  62.      *  
  63.      */  
  64.     @XmlElementDecl(namespace = "", name = "address")  
  65.     public JAXBElement<Address> createAddress(Address value) {  
  66.         return new JAXBElement<Address>(_Address_QNAME, Address.classnull, value);  
  67.     }  
  68.   
  69.     /** 
  70.      * Create an instance of {@link JAXBElement }{@code <}{@link Customer }{@code >}} 
  71.      *  
  72.      */  
  73.     @XmlElementDecl(namespace = "", name = "customer")  
  74.     public JAXBElement<Customer> createCustomer(Customer value) {  
  75.         return new JAXBElement<Customer>(_Customer_QNAME, Customer.classnull, value);  
  76.     }  
  77.   
  78. }  


JaxbDemo.java  
Java代码   收藏代码
  1. package demo.xml;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.OutputStream;  
  6.   
  7. import javax.xml.bind.JAXBContext;  
  8. import javax.xml.bind.JAXBElement;  
  9. import javax.xml.bind.Marshaller;  
  10. import javax.xml.bind.Unmarshaller;  
  11.   
  12. public class JaxbDemo {  
  13.       
  14.     public static void main(String[] args) throws Exception {  
  15.         unmarshalling();  
  16.         marshalling();  
  17.     }  
  18.       
  19.     static void unmarshalling() throws Exception{  
  20.         JAXBContext jc = JAXBContext.newInstance("demo.xml");   
  21.         Unmarshaller u = jc.createUnmarshaller();   
  22.         JAXBElement customerE = (JAXBElement) u.unmarshal(new FileInputStream(   
  23.                                 "F:\\xmldemo\\a.xml"));   
  24.         Customer bo = (Customer) customerE.getValue();   
  25.         System.out.println(bo.getName() + " : " + bo.getAddress().getCity() + " : " + bo.getId());  
  26.     }  
  27.       
  28.     static void marshalling()throws Exception{  
  29.         JAXBContext jc = JAXBContext.newInstance(Customer.class);   
  30.         Marshaller ms = jc.createMarshaller();  
  31.         ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
  32.           
  33.         Customer cust = new Customer();  
  34.         cust.setId("cust id");  
  35.         cust.setName("Marshall");  
  36.         Address a = new Address();  
  37.         a.setCity("suzhou");  
  38.         a.setStreet("jinjihulu");  
  39.         a.setZip(215200);  
  40.         cust.setAddress(a);  
  41.           
  42.         OutputStream os = new FileOutputStream("F:\\xmldemo\\a.xml");  
  43.         ms.marshal(cust, os);  
  44.         os.close();  
  45.     }  
  46.   
  47. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值