java 解析SOAP字串指定標籤轉換為實體類
1.測試實體類
package test;
@SuppressWarnings("serial")
public class Policy implements java.io.Serializable{
private String LicenseNo;
private String LicenseType;
private String ComCode;
private String InsuredName;
private long IdentifyNumber;
public Policy() {
super();
}
public String getLicenseNo() {
return LicenseNo;
}
public void setLicenseNo(String licenseNo) {
LicenseNo = licenseNo;
}
public String getLicenseType() {
return LicenseType;
}
public void setLicenseType(String licenseType) {
LicenseType = licenseType;
}
public String getComCode() {
return ComCode;
}
public void setComCode(String comCode) {
ComCode = comCode;
}
public String getInsuredName() {
return InsuredName;
}
public void setInsuredName(String insuredName) {
InsuredName = insuredName;
}
public long getIdentifyNumber() {
return IdentifyNumber;
}
public void setIdentifyNumber(long identifyNumber) {
IdentifyNumber = identifyNumber;
}
}
2.轉換工具類
package test;
import java.io.ByteArrayInputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* XML工具類
* @author suyunlong
*
*/
public class XmlUtil {
/**
* 解析XML轉換為Object
* @param strXML xml字串
* @param elementName 解析根標籤名
* @param className 類名全路徑(包名+類名)
* @return
*/
public static List<Object> parseObject(String strXML,String elementName,String className){
List<Object> list=new ArrayList<Object>();
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=null;
try{
builder=factory.newDocumentBuilder();
Document doc=builder.parse(new ByteArrayInputStream(strXML.getBytes("utf-8")));
NodeList nodelist=doc.getElementsByTagName(elementName);
for(int i=0;i<nodelist.getLength();i++){
Node node=nodelist.item(i);
NodeList chlist=node.getChildNodes();
Object bean=Class.forName(className).newInstance();
Class<?> cls=bean.getClass();
Method methods[]=cls.getDeclaredMethods();
Field fields[]=cls.getDeclaredFields();
for(int j=0;j<chlist.getLength();j++)
{
Node chnode=chlist.item(j);
if(chnode instanceof Element)
{
//System.out.println(chnode.getNodeName()+","+chnode.getTextContent());
for(Field field:fields)
{
String fieldName=field.getName();
if(fieldName.equals(chnode.getNodeName())){
String fldtype=field.getType().getSimpleName();
String setMethod=pareSetName(fieldName);
if(!checkMethod(methods,setMethod))
{
continue;
}
Object value=chnode.getTextContent();
Method method=cls.getMethod(setMethod,field.getType());
if(null != value)
{
if("String".equals(fldtype))
{
method.invoke(bean,value.toString());
}
else if("Date".equals(fldtype))
{
Date temp=parseDate(value.toString());
method.invoke(bean,temp);
}
else if("Integer".equals(fldtype) || "int".equals(fldtype))
{
Integer intval=Integer.parseInt(value.toString());
method.invoke(bean,intval);
}
else if("Long".equalsIgnoreCase(fldtype))
{
Long temp=Long.parseLong(value.toString());
method.invoke(bean,temp);
}
else if(fldtype.equalsIgnoreCase("Float"))
{
Float f=Float.parseFloat(value.toString());
method.invoke(bean,f);
}
else if("Double".equalsIgnoreCase(fldtype))
{
Double temp=Double.parseDouble(value.toString());
method.invoke(bean,temp);
}
else if("Boolean".equalsIgnoreCase(fldtype))
{
Boolean temp=Boolean.parseBoolean(value.toString());
method.invoke(bean,temp);
}
else
{
System.out.println("not supper type"+fldtype);
}
}
break;
}
}
}
}
list.add(bean);
}
}
catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
return list;
}
/**
* 拼接某屬性set 方法
* @param fldname
* @return
*/
public static String pareSetName(String fldname)
{
if(null==fldname || "".equals(fldname))
{
return null;
}
String pro="set"+fldname.substring(0,1).toUpperCase()+fldname.substring(1);
return pro;
}
/**
* 判斷該方法是否存在
* @param methods
* @param met
* @return
*/
public static boolean checkMethod(Method methods[],String met)
{
if(null != methods)
{
for(Method method:methods)
{
if(met.equals(method.getName()))
{
return true;
}
}
}
return false;
}
/**
* 格式化string為Date
* @param datestr
* @return date
*/
public static Date parseDate(String datestr)
{
if(null==datestr || "".equals(datestr))
{
return null;
}
try
{
String fmtstr=null;
if(datestr.indexOf(':')>0)
{
fmtstr="yyyy-MM-dd HH:mm:ss";
}
else
{
fmtstr="yyyy-MM-dd";
}
SimpleDateFormat sdf=new SimpleDateFormat(fmtstr);
return sdf.parse(datestr);
}
catch(Exception e)
{
System.out.println(e.getMessage());
return null;
}
}
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
String strXML="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<soapenv:Header xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" >" +
"<ns3:requesthead xmlns:ns3=\"http://pub.webservice.cmp.com\" xmlns:ns2=\"http://pan.prpall.webservice.cmp.com\">" +
"<ns3:request_type>Q101</ns3:request_type><ns3:uuid>8918211j-12121212</ns3:uuid>" +
"<ns3:sender>0545</ns3:sender><ns3:server_version>2.0</ns3:server_version>" +
"<ns3:user>0545</ns3:user><ns3:password>4005973D0841EAC706DC9B8B32858D9D</ns3:password>" +
"<ns3:areacode>45000000</ns3:areacode><ns3:ChnlNo>yntc</ns3:ChnlNo>" +
"<ns3:flowintime>2017-05-24 15:00:21</ns3:flowintime></ns3:requesthead>" +
"</soapenv:Header><soap:Body xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<ns2:GETORIGINPOLICYREQ xmlns:ns2=\"http://pan.prpall.webservice.cmp.com\" xmlns:ns3=\"http://pub.webservice.cmp.com\">" +
"<ns2:BIZ_ENTITY><Policy><LicenseNo>桂QW1231</LicenseNo>" +
"<LicenseType>02</LicenseType><ComCode>45010200</ComCode>" +
"<Resource>0524</Resource><InsuredName>寒冰</InsuredName>" +
"<IdentifyType>01</IdentifyType>" +
"<IdentifyNumber>220381199203164520</IdentifyNumber>" +
"<ChannelCode>000002000001</ChannelCode></Policy></ns2:BIZ_ENTITY>" +
"</ns2:GETORIGINPOLICYREQ></soap:Body></soapenv:Envelope>";
List list=XmlUtil.parseObject(strXML,"Policy","test.Policy");
for(int i=0;i<list.size();i++){
Policy policy=(Policy)list.get(i);
System.out.println(policy.getComCode()+","+policy.getIdentifyNumber()+","+
policy.getInsuredName()+","+policy.getLicenseNo()+","+policy.getLicenseType());
}
}
}
3.測試結果
45010200,220381199203164520,寒冰,桂QW1231,02
相關文章
- JavaScript html標籤轉義為實體字元JavaScriptHTML字元
- 萬能java字串編碼轉換工具類Java字串編碼
- VOC標籤轉化為YOLO標籤YOLO
- html標籤的尖括號轉碼為字元實體HTML字元
- 原始配置字串進行解析並轉換為字典字串
- Java如何將字串轉換為字元陣列?Java字串字元陣列
- 如何在Java中將字串轉換為日期Java字串
- 跳轉個人主頁的指定標籤內容
- NFC 標籤:自動跳轉到指定應用
- 劍指offer(Java版)--將字串轉換為整數Java字串
- Java 浮點到字串轉換Java字串
- json字串 轉換為陣列JSON字串陣列
- JavaScript 字串轉換為陣列JavaScript字串陣列
- 使用Wesky.Net.Opentools庫,一行程式碼實現實體類型別轉換為Json格式字串行程型別JSON字串
- Java String類,字串常量池,建立方法,字串的獲取,擷取,轉換,分割。Java字串
- java 字串與檔案相互轉換Java字串
- JavaScript將陣列轉換為字串JavaScript陣列字串
- JSON字串轉換為物件直接量JSON字串物件
- Thymeleaf將字串轉換為數字字串
- python字串轉換為日期時間Python字串
- 地圖POI類別標籤體系建設實踐地圖
- Java如何將Object轉換成指定Class物件JavaObject物件
- Java Stram實現Map和字串之間互相轉換| BaeldungJava字串
- GPS座標轉換為BIM
- pbootcms模板指定欄目標籤呼叫boot
- Spring Bean 標籤解析SpringBean
- JavaScript將物件轉換為JSON格式字串JavaScript物件JSON字串
- eval()將JSON格式字串轉換為物件JSON字串物件
- php日期時間如何轉換為字串PHP字串
- python實現字串轉換整數Python字串
- java工具類之編碼轉換工具類Java
- html標籤分類HTML
- JSTL標籤工具類JS
- Java 解析xml報文放入Map,並判斷所有xml標籤是否為空JavaXML
- JavaScript replace()替換字串中指定字元JavaScript字串字元
- JavaScript 替換字串全部指定內容JavaScript字串
- Java 正確的做字串編碼轉換Java字串編碼
- Java 中 CLOB 和字串之間的轉換Java字串
- Java SimpleDateFormat處理日期與字串的轉換JavaORM字串