通过webservice传来的XML是一个String类型串,解析此字符串的方法如下
1.将字符串的内容定义成一个bean
2.通过Persister 的read方法读取bean和xml
3.将读取的XML内容封装成CData类型
定义字符串内容BEAN类
package com.csValue.dirService.xmlAnalysis;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
/**
* @ClassName: TransferDataBean
* @Description: 客户端的出库数据(供应商)
* @author:dxl
* @date: 2015-08-13
* @see: V1.0
* @version: V1.0
*/
@Root
public class TransferDataBean {
@Element
private String supplier_drug_code; //药品编码
@Element
private String drug_name; //药品名称
@Element
private String package_spec; //包装规格
@Element
private String package_units; //包装单位
@Element
private String batch_no; //批次
@Element
private String expire_date; //效期
@Element
private String produce_date; //生产时间
@Element
private String manufact_name; //生产厂家
@Element
private String file_count; //药检单数量
@Element
private String supplier_name; //供应商名称
public String getSupplier_drug_code() {
return supplier_drug_code;
}
public void setSupplier_drug_code(String supplier_drug_code) {
this.supplier_drug_code = supplier_drug_code;
}
public String getDrug_name() {
return drug_name;
}
public void setDrug_name(String drug_name) {
this.drug_name = drug_name;
}
public String getPackage_spec() {
return package_spec;
}
public void setPackage_spec(String package_spec) {
this.package_spec = package_spec;
}
public String getPackage_units() {
return package_units;
}
public void setPackage_units(String package_units) {
this.package_units = package_units;
}
public String getBatch_no() {
return batch_no;
}
public void setBatch_no(String batch_no) {
this.batch_no = batch_no;
}
public String getExpire_date() {
return expire_date;
}
public void setExpire_date(String expire_date) {
this.expire_date = expire_date;
}
public String getManufact_name() {
return manufact_name;
}
public void setManufact_name(String manufact_name) {
this.manufact_name = manufact_name;
}
public String getFile_count() {
return file_count;
}
public void setFile_count(String file_count) {
this.file_count = file_count;
}
public String getSupplier_name() {
return supplier_name;
}
public void setSupplier_name(String supplier_name) {
this.supplier_name = supplier_name;
}
public String getProduce_date() {
return produce_date;
}
public void setProduce_date(String produce_date) {
this.produce_date = produce_date;
}
}
读取XML内容并封装成cdata类型
package com.csValue.dirService.xmlAnalysis;
import org.simpleframework.xml.core.Persister;
import ssdevframework.core.collection.CData;
public class XMLAnalysis {
public static CData getXMLData(String stringXml) {
Persister persister = new Persister();
CData cd=new CData();
try {
TransferDataBean info = persister.read(TransferDataBean.class, stringXml);// 解析XML文件
cd.set("supplierDrugCode", info.getSupplier_drug_code());
cd.set("drugName", info.getDrug_name());
cd.set("packageSpec", info.getPackage_spec());
cd.set("packageUnits", info.getPackage_units());
cd.set("expireDate", info.getExpire_date());
cd.set("produceDate", info.getProduce_date());
cd.set("batchNo", info.getBatch_no());
cd.set("manufactName", info.getManufact_name());
cd.set("fileCount", info.getFile_count());
cd.set("supplierName", info.getSupplier_name());
} catch (Exception e) {
e.printStackTrace();
}
return cd;
}
}
读取XML内容封装成CData 类型
@SuppressWarnings("static-access")
@WebMethod(operationName = "checkTransferData")
public String checkTransferData( String userId,
String pwd,
String stringXml
) {
//解析XML封装成CData
XMLAnalysis as=new XMLAnalysis();
CData drugCheckInfo = as.getXMLData(stringXml);
String strResult = dirService.checkTransferData(userId,pwd, drugCheckInfo);//校验药检单对应的药品信息是否正确
return strResult;
}
以上代码即可将XML内容读取出后set到CData对象中。