java soapenv_xml 字符串 标签为 soapenv:Envelope 转为 javaBean实体

这个Java工具类解析XML字符串,基于给定的根元素名称和目标类名,将XML内容映射到JavaBean实例。它使用DOM解析器处理XML,反射来设置JavaBean的属性,并能处理各种基本类型的数据转换。
摘要由CSDN通过智能技术生成

package com.hod.weibao.common.xml.ddbx;

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;

public class SoapUtil {

/**

* 解析XML转换为Object

* @param strXML xml字符串

* @param elementName 解析根标签名

* @param className 类名全路径(包名+类名)

* @return

*/

public static List parseObject(String strXML,String elementName,String className){

List list=new ArrayList();

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

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

{

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;

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值