`

[Java]XML与JAXB

阅读更多
a.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="cust id">
    <address>
        <street>jinjihulu</street>
        <city>suzhou</city>
        <zip>215200</zip>
    </address>
    <name>Marshall</name>
</customer>


a.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
   
    <xs:complexType name="Customer"> 
      <xs:sequence> 
         <xs:element name="address" type="Address"/> 
         <xs:element name="name" type="xs:string"/> 
      </xs:sequence> 
       <xs:attribute name="id" type="xs:string"/>       
    </xs:complexType> 
   
      <xs:complexType name="Address"> 
      <xs:sequence> 
         <xs:element name="street" type="xs:string"/> 
         <xs:element name="city" type="xs:string"/> 
         <xs:element name="zip" type="ZipCodeType"/> 
       </xs:sequence> 
   </xs:complexType> 
  
    <xs:simpleType name="ZipCodeType"> 
      <xs:restriction base="xs:integer"> 
         <xs:minInclusive value="10000"/> 
         <xs:maxInclusive value="99999"/> 
      </xs:restriction> 
    </xs:simpleType> 
    <xs:element name="customer" type="Customer"/> 
    <xs:element name="address" type="Address"/> 
</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
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2011.12.30 at 04:13:13 PM CST 
//


package demo.xml;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for Customer complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="Customer">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="address" type="{}Address"/>
 *         &lt;element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *       &lt;attribute name="id" type="{http://www.w3.org/2001/XMLSchema}string" />
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Customer", propOrder = {
    "address",
    "name"
})
public class Customer {

    @XmlElement(required = true)
    protected Address address;
    @XmlElement(required = true)
    protected String name;
    @XmlAttribute
    protected String id;

    /**
     * Gets the value of the address property.
     * 
     * @return
     *     possible object is
     *     {@link Address }
     *     
     */
    public Address getAddress() {
        return address;
    }

    /**
     * Sets the value of the address property.
     * 
     * @param value
     *     allowed object is
     *     {@link Address }
     *     
     */
    public void setAddress(Address value) {
        this.address = value;
    }

    /**
     * Gets the value of the name property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the value of the name property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setName(String value) {
        this.name = value;
    }

    /**
     * Gets the value of the id property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getId() {
        return id;
    }

    /**
     * Sets the value of the id property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setId(String value) {
        this.id = value;
    }

}




Address.java
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2011.12.30 at 04:13:13 PM CST 
//


package demo.xml;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for Address complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="Address">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="street" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="city" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="zip" type="{}ZipCodeType"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Address", propOrder = {
    "street",
    "city",
    "zip"
})
public class Address {

    @XmlElement(required = true)
    protected String street;
    @XmlElement(required = true)
    protected String city;
    protected int zip;

    /**
     * Gets the value of the street property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getStreet() {
        return street;
    }

    /**
     * Sets the value of the street property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setStreet(String value) {
        this.street = value;
    }

    /**
     * Gets the value of the city property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getCity() {
        return city;
    }

    /**
     * Sets the value of the city property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setCity(String value) {
        this.city = value;
    }

    /**
     * Gets the value of the zip property.
     * 
     */
    public int getZip() {
        return zip;
    }

    /**
     * Sets the value of the zip property.
     * 
     */
    public void setZip(int value) {
        this.zip = value;
    }

}




ObjectFactory.java
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2011.12.30 at 04:13:13 PM CST 
//


package demo.xml;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;


/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the generated package. 
 * <p>An ObjectFactory allows you to programatically 
 * construct new instances of the Java representation 
 * for XML content. The Java representation of XML 
 * content can consist of schema derived interfaces 
 * and classes representing the binding of schema 
 * type definitions, element declarations and model 
 * groups.  Factory methods for each of these are 
 * provided in this class.
 * 
 */
@XmlRegistry
public class ObjectFactory {

    private final static QName _Address_QNAME = new QName("", "address");
    private final static QName _Customer_QNAME = new QName("", "customer");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link Customer }
     * 
     */
    public Customer createCustomer() {
        return new Customer();
    }

    /**
     * Create an instance of {@link Address }
     * 
     */
    public Address createAddress() {
        return new Address();
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link Address }{@code >}}
     * 
     */
    @XmlElementDecl(namespace = "", name = "address")
    public JAXBElement<Address> createAddress(Address value) {
        return new JAXBElement<Address>(_Address_QNAME, Address.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link Customer }{@code >}}
     * 
     */
    @XmlElementDecl(namespace = "", name = "customer")
    public JAXBElement<Customer> createCustomer(Customer value) {
        return new JAXBElement<Customer>(_Customer_QNAME, Customer.class, null, value);
    }

}



JaxbDemo.java
package demo.xml;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class JaxbDemo {
	
	public static void main(String[] args) throws Exception {
		unmarshalling();
		marshalling();
	}
	
	static void unmarshalling() throws Exception{
		JAXBContext jc = JAXBContext.newInstance("demo.xml"); 
        Unmarshaller u = jc.createUnmarshaller(); 
        JAXBElement customerE = (JAXBElement) u.unmarshal(new FileInputStream( 
                                "F:\\xmldemo\\a.xml")); 
        Customer bo = (Customer) customerE.getValue(); 
        System.out.println(bo.getName() + " : " + bo.getAddress().getCity() + " : " + bo.getId());
	}
	
	static void marshalling()throws Exception{
		JAXBContext jc = JAXBContext.newInstance(Customer.class); 
		Marshaller ms = jc.createMarshaller();
		ms.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		
		Customer cust = new Customer();
		cust.setId("cust id");
		cust.setName("Marshall");
		Address a = new Address();
		a.setCity("suzhou");
		a.setStreet("jinjihulu");
		a.setZip(215200);
		cust.setAddress(a);
		
		OutputStream os = new FileOutputStream("F:\\xmldemo\\a.xml");
		ms.marshal(cust, os);
		os.close();
	}

}



分享到:
评论

相关推荐

    JAXB工具类 xml转为java对象 java对象转为xml

    JAXB工具类 xml转为java对象 java对象转为xml ,本人亲测,可以使用!!!

    通过JAXB实现完成java对象和xml的互相转换

    NULL 博文链接:https://xiongpan0807.iteye.com/blog/1832124

    JAXB技术实现xml与java对象互相转化代码教程

    使用java jdk的JAXB技术实现xml与java对象互相转化代码教程: JDK中JAXB相关的重要Class和Interface:(来源于百度百科JAXB) •JAXBContext类,是应用的入口,用于管理XML/Java绑定信息。 •Marshaller接口,将Java...

    用JAXB生成一个XML

    Java XML绑定架构(JAXB)提供了一个绑定编译器,xjc,来从一个XML模式中生成Java类。用JAXB的xjc生成的Java类代表了在XML模式中不同的元素和复杂类型(complexType)。(一个复杂类型通过指定属性和元素内的元素来提供对...

    JAXB完成XML和Java对象的互转

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。该过程中,JAXB也提供了将XML实例文档反向生成Java对象树的方法,并能将Java对象树的内容重新写到XML实例...

    JAXB 利用xsd文件生成java类

    编写xsd文件,利用jaxb生成java类。

    利用JAXB进行xml和javabean之间转换

    NULL 博文链接:https://luyuwww.iteye.com/blog/1988355

    JAXB XML TO JAVA

    JAXB XML TO JAVA,文件转化 生成java代码

    Java XML绑定技术 (Castor JAXB XMLBeans)

    本文系原创,如需转载请注明作者...本文详细介绍了Java XML绑定技术,并重点说明了如果使用JAXB来简化XML文档的操作。如果你的Java项目中需要使用XML(作为配置文件或数据库),不妨试一下JAXB,你一定会有惊喜的发现。

    jaxb 2.0 java architecture for xml binding

    THE Java™Architecture for XML Binding (JAXB) provides a fast and convenient way to bind between XML schemas and Java representations, making it easy for Java developers to incorporate XML data and ...

    JAVA与XML.rar

    《Java与XML》(第三版)的内容涵盖了所有主要的Java XML处理库程序,全面讲解了SAX、DOM、StAX、JDOM以及dom4j的应用程序编程接口,同时还囊括了最新版本的用于XML处理的Java应用程序编程接口(JAXP)和用于XML绑定...

    JAXB与xml相互转换实例

    JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术。JAXB与xml相互转换实例。

    Java and XML, 3rd Edition

    《Java与XML》(第三版)的内容涵盖了所有主要的Java XML处理库程序,全面讲解了SAX、DOM、StAX、JDOM以及dom4j的应用程序编程接口,同时还囊括了最新版本的用于XML处理的Java应用程序编程接口(JAXP)和用于XML绑定...

    java 解析XML性能对比分析Demo

    Java 解析XML性能分析Demo。包含了DOM解析,SAX解析, JDOM解析,DOM4J解析,JAXB解析例子。

    JAXB xml与对象转换

    jaxb将xml转换为对象或将对象转换为xml。测试通过。

    JAXB资料.rar

    JAXB_2.2_API.chm,Java+XML绑定技术.doc JAXB_2.2_API.chm,Java+XML绑定技术.doc JAXB_2.2_API.chm,Java+XML绑定技术.doc JAXB_2.2_API.chm,Java+XML绑定技术.doc JAXB_2.2_API.chm,Java+XML绑定技术.doc JAXB_2.2_...

    jaxb (XML操作)

    jaxb 将xml里面的对象转化为一个个类,大大地简化了xml的相关操作。unmarshal marshal

    JAXB 生成XML文件

    JAXB注解 java 关于xml的注解,自动生成xml文件

    JAXB_Java Architecture For XML Binding

    NULL 博文链接:https://rayoo.iteye.com/blog/1233534

    java 使用 JAXB 将xml转换为 bean 包含xml和dto和读取文件的util类

    java 使用 JAXB 将xml转换为 bean 包含xml和dto和读取文件的util类

Global site tag (gtag.js) - Google Analytics