/**
*
*/
package com.guangyue.xml;
import java.io.StringReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import com.guangyue.util.branch.Branch;
import com.guangyue.webservice.entity.IBaseFile;
import com.guangyue.webservice.util.Compress;
/**
* @author jt
*
*/
public class Test {
/**
*
*/
public Test() {
}
// 通过webservice取得xml字符串
public String LoadXMLString() {
String serviceResponse = "";
try {
Service serviceModel = new ObjectServiceFactory().create(IBaseFile.class);
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
String serviceUrl = "http://192.168.1.10:8080/smis/services/basefile";
IBaseFile client = null;
client = (IBaseFile) factory.create(serviceModel, serviceUrl);
serviceResponse = new String(Compress.unzip(client.getBranch()), "GB2312");
} catch (Exception e) {
serviceResponse = e.toString();
}
return serviceResponse;
}
/**
* @param args
* @throws NoSuchFieldException
* @throws ClassNotFoundException
*/
public static void main(String[] args) {
Test test = new Test();
String xmlstring = test.LoadXMLString();
List objlist;
try {
objlist = test.creatobj(xmlstring, Branch.class);
for (Iterator it = objlist.iterator(); it.hasNext();) {
Branch o = (Branch) it.next();
System.out.println(o.getName() + o.getShortname());
}
} catch (Exception e) {
e.printStackTrace();
}
/*
* XStream sm = new XStream(new DomDriver()); sm.alias("branch",
* com.guangyue.webservice.entity.Branch.class); try { List vt =
* (java.util.ArrayList)sm.fromXML(xmlstring);
* com.guangyue.webservice.entity.Branch
* bh=(com.guangyue.webservice.entity.Branch)vt.get(0);
* System.out.println(vt); } catch (Exception e) { e.printStackTrace(); }
*/
}
// 动态构造对象
public List creatobj(String xmlstring, Class objclass) throws Exception {
List returnlist = new ArrayList();
StringReader sr = new StringReader(xmlstring);
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
org.w3c.dom.Document doc;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(is);
NodeList nolist = doc.getDocumentElement().getChildNodes();
System.out.println("-------------------------->:" + xmlstring);
String cname = objclass.getName();
String Abstclname = cname.substring(0, cname.lastIndexOf(".") + 1) + "Abstract" + cname.substring(cname.lastIndexOf(".") + 1);
Field[] fileds = Class.forName(Abstclname).getDeclaredFields();
Map fieldmap = new HashMap();
for (int fi = 0; fi < fileds.length; fi++) {
fieldmap.put(fileds[fi].getName().toLowerCase(), fileds[fi]);
}
for (int i = 0; i < nolist.getLength(); i++) {
Node node = nolist.item(i);
if (node.getNodeType() == node.ELEMENT_NODE) {
NodeList childnolist = node.getChildNodes();
Object obj = new Object();
obj = objclass.getConstructor(new Class[] {}).newInstance(new Class[] {});
for (int j = 0; j < childnolist.getLength(); j++) {
Node childnode = childnolist.item(j);
String chndname = childnode.getNodeName();
if (childnode.getNodeType() == childnode.ELEMENT_NODE) {
String st5 = "";
if (childnode.hasChildNodes()) {
st5 = childnode.getFirstChild().getNodeValue();
}
Field fild = (Field) fieldmap.get(chndname.toLowerCase());
if (fild != null) {
Class[] classage = new Class[] { fild.getType() };
String methname = "set" + fild.getName().substring(0, 1).toUpperCase() + fild.getName().substring(1);
Method mth;
mth = Class.forName(Abstclname).getDeclaredMethod(methname, classage);
Object[] objargs = new Object[] {};
if (fild.getType() == Integer.class) {
Integer integ = Integer.valueOf(st5);
objargs = new Object[] { integ };
}
if (fild.getType() == String.class) {
String streg = st5;
objargs = new Object[] { streg };
}
if (fild.getType() == Short.class) {
Short shortreg = Short.valueOf(st5);
objargs = new Object[] { shortreg };
}
try {
mth.invoke(obj, objargs);
} catch (IllegalArgumentException e) {
e.printStackTrace();
throw new Exception("调用方法 :" + methname + "d的时候出错!");
}
}
}
}
returnlist.add(obj);
}
}
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
throw new Exception("加载xml的时候出错");
}
return returnlist;
}
}