日期:2014-05-17 浏览次数:20736 次
// 用递归方法遍历DOM树 public void GetElement(NodeList nodelist) { Node cnode; int i; String str; int len; if (nodelist.getLength() == 0) { return; } for (i = 0; i < nodelist.getLength(); i++) { cnode = nodelist.item(i); if (cnode.getNodeType() == 1) { if(cnode.hasChildNodes()){ NodeList childlist = cnode.getChildNodes(); if(childlist.item(i).getNodeType()==Node.ENTITY_NODE){ String stringLetter=cnode.getNodeName().substring(0, 1).toUpperCase(); String setName="set"+ stringLetter + cnode.getNodeName().substring(1); // Method setMethod=fs.getMethod(setName, cnode.getNodeType()); } } GetElement(cnode.getChildNodes()); } else if (cnode.getNodeType() == 3) { str = cnode.getNodeValue(); len = str.length(); if (len > 1){ String fieldName = cnode.getParentNode().getNodeName(); String stringLetter=fieldName.substring(0, 1).toUpperCase(); String setName="set"+stringLetter+fieldName.substring(1); Field[] fields1=head.getDeclaredFields(); Field[] fields2=msg.getDeclaredFields(); for (int j = 0; j < fields1.length; j++) { if(fields1[j].getName()==fieldName){ try { Method setMethod = head.getMethod(setName, fields1[j].getType().getClass()); setMethod.invoke(head, str); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } for (int j = 0; j < fields2.length; j++) { if(fields2[j].getName()==fieldName){ try { Method setMethod = msg.getMethod(setName, fields2[j].getType().getClass()); setMethod.invoke(msg, str); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block