webservice学习 3.JAXB的简单使用

http://zh.wikipedia.org/wiki/JAXB 写道
JAXB(Java Architecture for XML Binding简称JAXB)允许Java开发人员将Java类映射为XML表示方式。JAXB提供两种主要特性:将一个Java对象序列化为XML,以及反向操作,将XML解析成Java对象。换句话说,JAXB允许以XML格式存储和读取数据,而不需要程序的类结构实现特定的读取XML和保存XML的代码。

当规格复杂且经常变化时JAXB特别有用。在这种情况下,经常修改XML模式定义以与Java定义保持一致很花费时间并且容易产生错误。

JAXB是Java EE平台的API之一,同时是Java Web服务开发包 (JWSDP)的一部分。 JAXB也是Web服务互操作性技术(WSIT)的基础之一. JAXB是J2SE1.6的一部分。

JAXB 1.0是在Java社群过程中作为JSR 31开发出来的。JAXB 2.0是作为JSR 222开发的。这两个版本的参考实现可以从java.net下载。

 在CXF(2.7.0)里面的定义和实现包是以下两个:



 

在使用上个人感觉和xstream等工具是很类似的 毕竟实现对于使用者来说是透明的.

 

以下是一个简单的例子代码 因为是第一次写难免写错不要介意:

package org.cc.jaxb;

import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

public class Client {
  public static void main(String[] args) throws JAXBException {
	ObjectFactory factory=new ObjectFactory();
	Person person=factory.createPerson();
	Address address=new Address();
	address.setCountry("中国");
	address.setStreet("西湖区");
	address.setHouse("xxx街道xx号");
	person.setAge(22);
	person.setBirthDate(new Date());
	person.setName("cc");
	person.setSex(true);
	person.setTempId(1);
	person.setAddress(address);
	
	List<String> favors=new ArrayList<String>();
	favors.add("吃饭");
	favors.add("睡觉");
	person.setFavors(favors);
	JAXBContext jaxbContext=JAXBContext.newInstance("org.cc.jaxb");
	Marshaller marshaller=jaxbContext.createMarshaller();
	
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
	marshaller.setProperty(Marshaller.JAXB_ENCODING, "gbk");

	marshaller.marshal(person, baos);
	    
	StringBuffer sb = new StringBuffer(new String(baos.toByteArray()));
	
	System.out.println(sb.toString());
	
	
	Unmarshaller unmarshaller=jaxbContext.createUnmarshaller();
	StringReader reader = new StringReader(sb.toString());
	Person per = (Person)unmarshaller.unmarshal(reader);
	System.out.println(per.getFavors());
}
}

 

package org.cc.jaxb;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Address {
   private String country;
	
	private String street;
	
	private String house;

	public String getCountry() {
		return country;
	}

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

	public String getStreet() {
		return street;
	}

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

	public String getHouse() {
		return house;
	}

	public void setHouse(String house) {
		this.house = house;
	}
}

 

package org.cc.jaxb;

import java.util.Date;
import java.util.List;

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.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement(name="Person") // 根名叫做Person
@XmlAccessorType(XmlAccessType.FIELD) //非static类型和标明transient的会被处理
public class Person {
    @XmlAttribute //作为属性
	public String name;
    @XmlAttribute //作为属性
    private int age;
    @XmlAttribute //作为属性
	private boolean sex;
    @XmlElement  //作为一个元素
	private Date birthDate;
	@XmlTransient // 标明不处理这个
	private long tempId;
	
	private Address address; //另一个类型的对象
	
	@XmlElementWrapper(name="list") //favors外包一层list
	private List<String> favors;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public boolean isSex() {
		return sex;
	}
	public void setSex(boolean sex) {
		this.sex = sex;
	}
	public Date getBirthDate() {
		return birthDate;
	}
	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}
	public long getTempId() {
		return tempId;
	}
	public void setTempId(long tempId) {
		this.tempId = tempId;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public List<String> getFavors() {
		return favors;
	}
	public void setFavors(List<String> favors) {
		this.favors = favors;
	}
}

 

package org.cc.jaxb;

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

@XmlRegistry
public class ObjectFactory {
	private final static QName _Person_QName =new QName("http://fair-jm.iteye.com","Person");
	private final static QName _Adress_QName =new QName("http://fair-jm.iteye.com","Address");
	
	public Person createPerson(){
		return new Person();
	}
	
	public Address createAddress(){
		return new Address();
	}
	
	@XmlElementDecl(namespace="http://fair-jm.iteye.com",name="Person")
	public JAXBElement<Person> createPerson(Person value){
		return new JAXBElement<Person>(_Person_QName, Person.class, value);
	}
	
	@XmlElementDecl(namespace="http://fair-jm.iteye.com",name="Address")   
	public JAXBElement<Address> createAddress(Address value){
		return new JAXBElement<Address>(_Adress_QName,Address.class,value);
	}
	
}

 输出结果:

<?xml version="1.0" encoding="gbk" standalone="yes"?>
<Person name="cc" age="22" sex="true" xmlns:ns2="http://fair-jm.iteye.com">
    <birthDate>2013-05-12T14:21:48.486+08:00</birthDate>
    <address>
        <country>中国</country>
        <house>xxx街道xx号</house>
        <street>西湖区</street>
    </address>
    <list>
        <favors>吃饭</favors>
        <favors>睡觉</favors>
    </list>
</Person>

[吃饭, 睡觉]

 

 

 

你可能感兴趣的:(webservice)