把 xml 解析为Java类

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;

import javax.xml.bind.annotation.XmlRootElement;

import org.compass.core.util.reader.StringReader;

@XmlRootElement

public class CommPayment {

private String service;//

private String input_charset;// 参数编码字符集 String

private String sign_type;//

private String sign;//  String

private String notify_time;// 

private String appid;// 

private String batch_no;// 

private String trade_no;//

private String subject;// 商品名称 String

private String seller_id;// 

private String buyer_id;// 商户系统买家id String

private String total_fee;// 交易金额 Number

private String order_type;// 

private String product_id;// 

private String pay_type;// 

private String income_fee;// 

private String income_mode;// 

private String process_result;// 交易系统支付处理结果 String

private String process_option;// 交易系统支付处理反馈意见 String

private String error_code;// 错误码 String

private String error_message;// 错误信息 String

public void setError_message(String error_message) {

this.error_message = error_message;

}

public String getService() {

return service;

}

public String getInput_charset() {

return input_charset;

}

public String getSign_type() {

return sign_type;

}

public String getSign() {

return sign;

}

public String getNotify_time() {

return notify_time;

}

public String getAppid() {

return appid;

}

public String getBatch_no() {

return batch_no;

}

public String getTrade_no() {

return trade_no;

}

public String getSubject() {

return subject;

}

public String getSeller_id() {

return seller_id;

}

public String getBuyer_id() {

return buyer_id;

}

public String getTotal_fee() {

return total_fee;

}

public String getOrder_type() {

return order_type;

}

public String getProduct_id() {

return product_id;

}

public String getPay_type() {

return pay_type;

}

public String getIncome_fee() {

return income_fee;

}

public String getIncome_mode() {

return income_mode;

}

public String getProcess_result() {

return process_result;

}

public String getProcess_option() {

return process_option;

}

public String getError_code() {

return error_code;

}

public String getError_message() {

return error_message;

}

public void setService(String service) {

this.service = service;

}

public void setInput_charset(String input_charset) {

this.input_charset = input_charset;

}

public void setSign_type(String sign_type) {

this.sign_type = sign_type;

}

public void setSign(String sign) {

this.sign = sign;

}

public void setNotify_time(String notify_time) {

this.notify_time = notify_time;

}

public void setAppid(String appid) {

this.appid = appid;

}

public void setBatch_no(String batch_no) {

this.batch_no = batch_no;

}

public void setTrade_no(String trade_no) {

this.trade_no = trade_no;

}

public void setSubject(String subject) {

this.subject = subject;

}

public void setSeller_id(String seller_id) {

this.seller_id = seller_id;

}

public void setBuyer_id(String buyer_id) {

this.buyer_id = buyer_id;

}

public void setTotal_fee(String total_fee) {

this.total_fee = total_fee;

}

public void setOrder_type(String order_type) {

this.order_type = order_type;

}

public void setProduct_id(String product_id) {

this.product_id = product_id;

}

public void setPay_type(String pay_type) {

this.pay_type = pay_type;

}

public void setIncome_fee(String income_fee) {

this.income_fee = income_fee;

}

public void setIncome_mode(String income_mode) {

this.income_mode = income_mode;

}

public void setProcess_result(String process_result) {

this.process_result = process_result;

}

public void setProcess_option(String process_option) {

this.process_option = process_option;

}

public void setError_code(String error_code) {

this.error_code = error_code;

}

public static void main(String[] args) {

String xml = "<?xml version=\"1.0\" encoding=\"GBK\"?><tjcom><service>BEST_bond</service><input_charset>GBK</input_charset><sign_type>MD5</sign_type><sign>6489a3bc5104d29bd5b2eb75630e8c02</sign><input_time>2016-08-03 14:49:55</input_time><appid>boceyun</appid><batch_no>16080300798000</batch_no><user_id>1111110145</user_id><product_id>gqwc5498</product_id><subject>gao普通0802</subject><bond>-1.00</bond><poundage>0.00</poundage><BEST_password></BEST_password><remark>不开票,100%货款</remark><trade_type>2</trade_type><product_type>2</product_type></tjcom>";

               //注意 root节点一定要是类的名称,并且首字母小写

String rXml =xml.replaceAll("tjcom", "commPayment");

System.out.println(rXml);

try {

//CommPayment.class 转化的类型

JAXBContext context = JAXBContext.newInstance(CommPayment.class);

Unmarshaller unmarshaller = context.createUnmarshaller();

CommPayment student = (CommPayment) unmarshaller

.unmarshal(new StringReader(rXml));

System.out.println(student.getService());

System.out.println(student.getInput_charset());

} catch (JAXBException e) {

e.printStackTrace();

}

}

}

猜你喜欢

转载自gjp014.iteye.com/blog/2354861
今日推荐