`
caiying0504
  • 浏览: 334125 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

WebService CXF学习(进阶篇4):JAXB剖析

阅读更多
前面几节我们讲解对象传递,但是通常情况下我们不直接传对象,因为直接传递对象安全性差,而且暴露了实体对象。所以我们选择传递XML文件,当然也可以传递JSON对象。这节我只针对传递XML,那么JAVA绑定成XML,服务端将XML解析成Java对象有什么工具可用吗,其实这样的工具多的是。这里我选择一个比较简单的JAXB工具来讲解一下。
    JAXB(Java Architecture for XML Binding)提供了一个快速而方便的方式绑定XML Schemas和java,使java程序员能够很方便的在java应用程序中处理XML数据。JAXB提供了将XML文档解组为java内容树的方法,以及将java内容树重新编组回XML文档的方法。JAXB同样也提供了一种从java对象生成XML Schema的方式。
    这里有几个重要的定义:
    编组(Marshalling)是把内存中的数据转化到存储媒介上的过程。因此在 Java 和 XML 环境中,编组就是把一些 Java 对象转化成一个(或多个) XML 文档。在数据库环境中,则是把 Java 表示的数据存入数据库。显然,编组的秘密在于把 Java 实例中的面向对象结构转化成适用于 XML 的 扁平结构,或者 RDBMS 中的关系结构(使用 Java 技术转换到 OODBMS 实际上很简单)。工作原理如下图所示:



    解组(Unmarshalling) 是把数据从存储媒介转换到内存中的过程--正好与编组相反。因此需要把 XML 文档解组到 Java VM 中。这里的复杂性不是在扁平数据中,因为这不是必需的,而在于从正确的数据到正确的 Java 代码变量的映射。如果映射是错误的,就不可能正确地访问数据。当然,如果再尝试重新编组还会造成更大的问题,并且问题传播得很快。工作原理如下图所示:




    往返(Round-tripping)可能是最重要也最容易误解的数据绑定术语。往返用于描述从存储媒介到内存然后回到存储媒介的完整循 环。在 XML 和 Java 技术环境中,这就意味着从 XML 文档到 Java 实例变量,然后再回到 XML 文档。正确的往返要求,如果中间没有修改数据,XML 输入和 XML 输出应该是等同的。
    下载地址:http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
  
    我们还以例子来说明它的工作原理,直观点。
    第一步,创建一个Customer对象
    Java代码 
@XmlRootElement(name="customer")   
@XmlAccessorType(XmlAccessType.FIELD)   
@XmlType(name = "")   
public class Customer {   
  
    @XmlAttribute(required = true)   
    protected String name;   
    @XmlAttribute(required = true)   
    protected int age;   
  
    /**  
     * 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 age property.  
     *   
     */  
    public int getAge() {   
        return age;   
    }   
  
    /**  
     * Sets the value of the age property.  
     *   
     */  
    public void setAge(int value) {   
        this.age = value;   
    }   
  
}   
  
      

@XmlRootElement(name="customer")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public class Customer {

    @XmlAttribute(required = true)
    protected String name;
    @XmlAttribute(required = true)
    protected int age;

    /**
     * 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 age property.
     * 
     */
    public int getAge() {
        return age;
    }

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

}
   

    第二步,创建一个测试类
Java代码 
public class SoapClient {   
  
    private final static String MODEL = "com.itdcl.model";   
    public static void main(String[] args) throws ParserConfigurationException, JAXBException, TransformerException{   
  
           
        ObjectFactory factory = new ObjectFactory();   
        Customer customer = factory.createCustomer();   
        customer.setAge(20);   
        customer.setName("Josen");   
  
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();   
        dbf.setNamespaceAware(true);   
        DocumentBuilder db = dbf.newDocumentBuilder();   
        Document doc = db.newDocument();   
  
        JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);   
        //Java对象转换成XML   
                Marshaller marshaller = jaxbContext.createMarshaller();   
        marshaller.marshal(customer, doc);   
           
        DOMSource domSource = new DOMSource(doc);   
        StringWriter writer = new StringWriter();   
        StreamResult result = new StreamResult(writer);   
        TransformerFactory tf = TransformerFactory.newInstance();   
        Transformer transformer = tf.newTransformer();   
        transformer.transform(domSource, result);   
        String xmlString = writer.toString();   
        System.out.println(xmlString);   
        //XML转换成Java对象   
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();   
        StringReader reader = new StringReader(xmlString);   
        Customer cus = (Customer)unmarshaller.unmarshal(reader);   
        System.out.println("Age:"+cus.getAge());   
        System.out.println("Name:"+cus.getName());   
           
           
    }   
}  

public class SoapClient {

	private final static String MODEL = "com.itdcl.model";
	public static void main(String[] args) throws ParserConfigurationException, JAXBException, TransformerException{

		
		ObjectFactory factory = new ObjectFactory();
		Customer customer = factory.createCustomer();
		customer.setAge(20);
		customer.setName("Josen");

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setNamespaceAware(true);
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc = db.newDocument();

		JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);
		//Java对象转换成XML
                Marshaller marshaller = jaxbContext.createMarshaller();
		marshaller.marshal(customer, doc);
		
		DOMSource domSource = new DOMSource(doc);
		StringWriter writer = new StringWriter();
		StreamResult result = new StreamResult(writer);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		transformer.transform(domSource, result);
		String xmlString = writer.toString();
		System.out.println(xmlString);
		//XML转换成Java对象
		Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
		StringReader reader = new StringReader(xmlString);
		Customer cus = (Customer)unmarshaller.unmarshal(reader);
		System.out.println("Age:"+cus.getAge());
		System.out.println("Name:"+cus.getName());
		
		
	}
}

      第三步,运行一个测试类,看看效果如何。Java与XML之间转换如此简单

      编组操作:利用上面生成的java文件执行编组操作。
Java代码 
JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);   
//Java对象转换成XML   
              Marshaller marshaller = jaxbContext.createMarshaller();   
marshaller.marshal(customer, doc);   
  
DOMSource domSource = new DOMSource(doc);   
StringWriter writer = new StringWriter();   
StreamResult result = new StreamResult(writer);   
TransformerFactory tf = TransformerFactory.newInstance();   
Transformer transformer = tf.newTransformer();   
transformer.transform(domSource, result);   
String xmlString = writer.toString();   
System.out.println(xmlString);  

		JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);
		//Java对象转换成XML
                Marshaller marshaller = jaxbContext.createMarshaller();
		marshaller.marshal(customer, doc);
		
		DOMSource domSource = new DOMSource(doc);
		StringWriter writer = new StringWriter();
		StreamResult result = new StreamResult(writer);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		transformer.transform(domSource, result);
		String xmlString = writer.toString();
		System.out.println(xmlString);

     解组操作:通过xml文件执行解组操作。
    Java代码 
             JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);   
              Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();   
StringReader reader = new StringReader(xmlString);   
Customer cus = (Customer)unmarshaller.unmarshal(reader);   
System.out.println("Age:"+cus.getAge());   
System.out.println("Name:"+cus.getName());  

                JAXBContext jaxbContext = JAXBContext.newInstance(MODEL);
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
		StringReader reader = new StringReader(xmlString);
		Customer cus = (Customer)unmarshaller.unmarshal(reader);
		System.out.println("Age:"+cus.getAge());
		System.out.println("Name:"+cus.getName());

   也可通过Ant配置来解组,如下:
Java代码 
<?xml version="1.0" encoding="utf-8" ?>   
<project default="xjc-compile" basedir=".">   
    <property name="src.dir" location="src" />   
    <property name="lib.dir" location="E:/cxf-lib" />   
    <property name="xml-schema.dir" location="src/WEB-INF" />   
    <property name="schema.name" value="cxfdemo.xsd" />   
    <property name="package" value="com.itdcl.model" />   
  
    <path id="classpath">   
        <fileset dir="${lib.dir}" includes="*.jar" />   
    </path>   
    <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"  
        classpathref="classpath" />   
           
    <target name="xjc-compile">   
        <echo message="Build Jaxb Class from Schema" />   
        <xjc schema="${xml-schema.dir}/${schema.name}"  
            destdir="${src.dir}" package="${package}" >   
            <produces dir="src/com/itdcl/model" includes="*" />   
        </xjc>   
    </target>   
</project>  
分享到:
评论
2 楼 84707227 2011-08-28  
和这边文章同一个系列的文章都是很多问题
1 楼 84707227 2011-08-28  
写这篇文章的作者可以审核一下吗?里面的问题太多了

相关推荐

Global site tag (gtag.js) - Google Analytics