利用jaxb将XML转化为POJO

在Java client中对restful webservice发送http post请求,将得到的请求转化为java pojo。
restful ws返回信息格式为

<RequestPhysicalInventoryPollingMessage>
<interval>int</interval>
<from>String</from>
<to>String</to>
<pollingurl>
String
</pollingurl>
</RequestPhysicalInventoryPollingMessage>


那么为了将这个xml转化为POJO,需要先创建这个POJO类:

package com.client.jaxb;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "RequestPhysicalInventoryPollingMessage")
public class RequestPhysicalInventoryPollingMessage {
String interval;
String from;
String to;
String pollingurl;

public RequestPhysicalInventoryPollingMessage() {
interval = "c";
from = "a";
to = "b";
}

public String getInterval() {
return interval;
}

public void setInterval(String interval) {
this.interval = interval;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;

}

public String getPollingurl() {
return pollingurl;
}

public void setPollingurl(String pollingurl) {
this.pollingurl = pollingurl;
}

}


[color=red]只需要在定义类的前面注明@XmlRootElement(name = "RequestPhysicalInventoryPollingMessage")[/color]
如果在每个属性前面加@XmlAttribute,则会报错。


package com.client.jaxb;

import java.io.ByteArrayInputStream;
import java.io.File;

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

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.FileRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

import com.client.RequestPhysicalInventoryPollingMessage;

public final class Client {

private Client() {
}

public static void main(String args[]) throws Exception {
//read the request payload
File input = new File(
"/RequestPhysicalInventory_FLOW_PIPeR_MPI3.1.xml");
//set the uri which we will send request and the http method is post
PostMethod post = new PostMethod(
"http://10.10.27.74:7001/ManagePhysicalInventoryConsumerProject/ManagePhysicalInventoryConsumerProxyService ");
post.addRequestHeader("Accept", "text/xml");
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
post.setRequestEntity(entity);
HttpClient httpclient = new HttpClient();
//execute the post method and get the response status code
int result = httpclient.executeMethod(post);
System.out.println("Response status code: " + result);
System.out.println("Response body: ");
//print the body of response
System.out.println(post.getResponseBodyAsString());

//invoke the method to convert the xml to a pojo
RequestPhysicalInventoryPollingMessage message = xml2pojo(post
.getResponseBodyAsString());
System.out.println("interval:" + message.getInterval());
System.out.println("from:" + message.getFrom());
System.out.println("to:" + message.getTo());
System.out.println("pollinguri:" + message.getPollingurl());
}
//convert the xml to a pojo
public static RequestPhysicalInventoryPollingMessage xml2pojo(String xml)
throws JAXBException {
JAXBContext jaxbContext;
//set the pojo in a new instance of JaxbContext
jaxbContext = JAXBContext
.newInstance(RequestPhysicalInventoryPollingMessage.class);

Unmarshaller um = jaxbContext.createUnmarshaller();
ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
/*
* this method has no parameter as String,so we must get the bytes from a String,and create a ByteArrayInputStream
* by the bytes.
*/
RequestPhysicalInventoryPollingMessage message = (RequestPhysicalInventoryPollingMessage) um
.unmarshal(is);
return message;
}

}


因为英语很烂,不知道用英语写的注释有没有写错。。。中文重新说明下:
RequestPhysicalInventoryPollingMessage message = (RequestPhysicalInventoryPollingMessage) um
.unmarshal(is);
这个方法:unmarshal() ,他不能接受String类型的参数(个人很不理解,为什么jaxb不去实现简单的把xml写在String中的POJO转化)。所以如果现在有一个String,内容是xml,就必须先调用这个String的getBytes()方法,然后用得到的byte[]去创建一个ByteArrayInputStream。然后把这个InputStream作为参数调用unmarshal().

执行代码结果如下:

Response status code: 200
Response body:
<?xml version="1.0" encoding="UTF-8"?>
<RequestPhysicalInventoryPollingMessage><interval>-1</interval><from>http://www.altova.com</from><to>http://www.altova.com</to><pollingurl>http://10.10.27.74:7001/ManagePhysicalInventoryConsumerProject/ManagePhysicalInventoryConsumerProxyService/String</pollingurl></RequestPhysicalInventoryPollingMessage>
interval:-1
from:http://www.altova.com
to:http://www.altova.com
pollinguri:http://10.10.27.74:7001/ManagePhysicalInventoryConsumerProject/ManagePhysicalInventoryConsumerProxyService/String

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值