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 实际上很简单)。工作原理如下图所示:
[img]http://dl.iteye.com/upload/attachment/190852/dd6865e1-8362-3398-9053-15e073cc5e15.gif[/img]


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

[img]http://dl.iteye.com/upload/attachment/190851/0fd17384-b10b-395d-b0a9-08a2699ad14f.gif[/img]


往返(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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值