面向对象读取xml文件的内容是出现问题 本帖最后由 chengcc2010 于 2011-06-15 16:38:26 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 大概看了下,这种问题一般把Object o1 = null; 改成 Object o1 = new Object();就行了. Reflect类:public class Reflect { public static final int METHODType_GET = 1; public static final int METHODType_SET = 0; private Object object; public Reflect(Object object) { this.object = object; } /** * 得到类名 * * @return */ public String getName() { return object.getClass().getSimpleName(); } /** * 打印一个map对象 * * @param mm */ public void print(Map<String, String> m) { Iterator<String> it = m.keySet().iterator(); while (it.hasNext()) { String a = it.next(); System.out.print(a); System.out.println(m.get(a)); } } /** * 得到一个实体对象的字段名 * * @return */ public Set<String> getFieldName() { Field[] fieldNames = object.getClass().getDeclaredFields(); Set<String> s = new HashSet<String>(); for (int i = 0; i < fieldNames.length; i++) { if (fieldNames[i].getModifiers() == 2) { String fieldName = fieldNames[i].getName(); s.add(fieldName); } } return s; } /** * 得到对象的各个私有字段的各个方法 * * @param methodType * 方法类型 * @return */ public Set<String> getMethodName(int methodType) { Set<String> f = getFieldName(); Set<String> m = new HashSet<String>(); Iterator<String> it = f.iterator(); while (it.hasNext()) { String a = it.next(); String Method = getMethodByFieldName(methodType, a); m.add(Method); } return m; } /** * 得到一个类的字段名和该字段的值,执行了get方法 * * @return */ public Map<String, String> getFieldNameAndValue() { Set<String> f = getFieldName(); Map<String, String> m = new HashMap<String, String>(); Iterator<String> it = f.iterator(); while (it.hasNext()) { String fieldName = it.next(); String method = getMethodByFieldName(METHODType_GET, fieldName); String fieldValue = getFieldValueByGetMethod(method); m.put(fieldName, fieldValue); } return m; } /** * 将一个map解析成一个对象 * * @param object * @param fieldValue * @throws Exception * @throws SecurityException */ public Object setFieldNameAndValue(Object object,Map<String, String> fieldValue) throws Exception { // 通过map中的字段得到object中的相应的舌头方法,然后把map中的值设进去返回该对象 Iterator<String> it = fieldValue.keySet().iterator(); while (it.hasNext()) { String field = it.next(); String method = getMethodByFieldName(METHODType_SET, field); Method m = object.getClass().getMethod(method, new Class[] { String.class }); m.invoke(object, new Object[] { fieldValue.get(field) }); //System.out.print(field); //System.out.println(fieldValue.get(field)); } return object; } /** * 通过字段名得到相应的set,get方法名 * * @param methodType * @param fieldName * @return */ private String getMethodByFieldName(int methodType, String fieldName) { String methodName = ""; if (methodType == METHODType_GET) { methodName = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); } else if (methodType == METHODType_SET) { methodName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); } return methodName; } /** * 通过方法名得到相应字段的值 * * @param methodName * @return * @throws Exception */ private String getFieldValueByGetMethod(String methodName) { Method m = null; String value = ""; try { m = object.getClass().getMethod(methodName); value = (String) m.invoke(object); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return value; } /*public String setFieldValueBySetMethod(String methodName, String value) { Method m; // String value = ""; try { m = object.getClass().getMethod(methodName); value = (String) m.invoke(object, new Object[] { value }); System.out.println("反射 set方法" + value); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return value; }*/} 楼主代码过长,但是我觉得要是你的xml结构良好的话在你拿到RootElement后,用它去取Linkman得到的list肯定是5个元素的然后你在分别去你的Linkman底下取详细信息就行,层次一定要搞清楚。 如果一个对象解析完成后马上输出的,不把他放到Vector中,就能把五个对象全部输入。一旦放到vector中,结果五个对象全部变成id为5的对象。 只要你能解析出对象,放入Vector中肯定是不会覆盖的,要放入Set中则需要重写equals方法!楼主要首先确定解析出的5个对象没问题,再看为什么放入容器时出错了 仔细看了看,终于发现问题了,问题出在你所有的Object都是同一个实例... /** * 变量初试化 */ Object o = co.newInstance();/*你的问题就在这里了, 你在循环体外面创建的实例, 循环里面add的o1只是o的引用, 改变的也一直是o里面的值, 最终的vector里面保存的事实上5个都是o,所以出现了你的问题.非常抱歉在一楼没有仔细看就回答了.事实上应该是 Object o1 = co.newInstance();然后把后面的 o1 = r.setFieldNameAndValue(o, xmlfieldValue);改成 o1 = r.setFieldNameAndValue(o1, xmlfieldValue);就行了*/... 试了下,没用,传入o只是要得到对象o的私有字段。不是用来装对象的。这个类似于hibernate中,HQL中的Query的list方法。 你确定你试了?//你在你自己的这个方法前打印传入的object,之后打印传出的object就明白了,//从你的这个方法你应该能发现你一直在对同一个object去setValuepublic Object setFieldNameAndValue(Object object, Map<String, String> fieldValue) throws Exception { System.out.println("传入的O : "+object); // 通过map中的字段得到object中的相应的舌头方法,然后把map中的值设进去返回该对象 Iterator<String> it = fieldValue.keySet().iterator(); while (it.hasNext()) { String field = it.next(); String method = getMethodByFieldName(METHODType_SET, field); Method m = object.getClass().getMethod(method, new Class[] { String.class }); m.invoke(object, new Object[] { fieldValue.get(field) }); // System.out.print(field); // System.out.println(fieldValue.get(field)); } System.out.println("传出的O : "+object); return object; }并且我试过我改的代码了,确认可行if (oobjectNodeList != null && oobjectNodeList.getLength() > 0) { for (int i = 0; i < oobjectNodeList.getLength(); i++) { Node node = oobjectNodeList.item(i); Object o1 = co.newInstance(); if (node.getNodeType() == Node.ELEMENT_NODE) { Iterator<String> it = s.iterator(); while (it.hasNext()) { String field = it.next(); Element e = (Element) node; NodeList nodeList = e .getElementsByTagName(field); String fieldValue = nodeList.item(0) .getChildNodes().item(0).getNodeValue(); xmlfieldValue.put(field, fieldValue); } o1 = r.setFieldNameAndValue(o1, xmlfieldValue); // 测试是否全部解析,结果全部解析了 System.out.println(o1.toString()); } objectList.add(o1); } // 这个管取第几个对象,结果都是id为5的那个对象 System.out.println(objectList.get(1).toString()); }/*输出结果Linkman [id=1, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]Linkman [id=2, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]Linkman [id=3, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]Linkman [id=4, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]Linkman [id=5, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]Linkman [id=2, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]*/ 如何学习Java?我从来没有接触过Java 奇怪的类型转换!!! java怎么向SQLServer里插入datetime类型? JMF有多少人了解 能不能给在下讲解一下呢 applet与servlet没法交互通信 怎样使JTextField的长度值随窗口变化而变化 简单问题--关于Color对象的使用 请教一个读文件的问题 谁告诉我,JBuilder5从哪下载较好? 请问JAVA PLUG-IN FOR IE的具体的下载网址? 内部类错误 swing小问题,求大侠们帮忙
public static final int METHODType_SET = 0;
private Object object; public Reflect(Object object) {
this.object = object;
} /**
* 得到类名
*
* @return
*/
public String getName() {
return object.getClass().getSimpleName();
} /**
* 打印一个map对象
*
* @param mm
*/
public void print(Map<String, String> m) {
Iterator<String> it = m.keySet().iterator();
while (it.hasNext()) {
String a = it.next();
System.out.print(a);
System.out.println(m.get(a));
}
} /**
* 得到一个实体对象的字段名
*
* @return
*/
public Set<String> getFieldName() {
Field[] fieldNames = object.getClass().getDeclaredFields();
Set<String> s = new HashSet<String>();
for (int i = 0; i < fieldNames.length; i++) {
if (fieldNames[i].getModifiers() == 2) {
String fieldName = fieldNames[i].getName();
s.add(fieldName);
}
}
return s;
} /**
* 得到对象的各个私有字段的各个方法
*
* @param methodType
* 方法类型
* @return
*/
public Set<String> getMethodName(int methodType) {
Set<String> f = getFieldName();
Set<String> m = new HashSet<String>();
Iterator<String> it = f.iterator();
while (it.hasNext()) {
String a = it.next();
String Method = getMethodByFieldName(methodType, a);
m.add(Method);
} return m; } /**
* 得到一个类的字段名和该字段的值,执行了get方法
*
* @return
*/
public Map<String, String> getFieldNameAndValue() {
Set<String> f = getFieldName();
Map<String, String> m = new HashMap<String, String>();
Iterator<String> it = f.iterator();
while (it.hasNext()) {
String fieldName = it.next();
String method = getMethodByFieldName(METHODType_GET, fieldName);
String fieldValue = getFieldValueByGetMethod(method);
m.put(fieldName, fieldValue);
} return m;
} /**
* 将一个map解析成一个对象
*
* @param object
* @param fieldValue
* @throws Exception
* @throws SecurityException
*/
public Object setFieldNameAndValue(Object object,Map<String, String> fieldValue) throws Exception {
// 通过map中的字段得到object中的相应的舌头方法,然后把map中的值设进去返回该对象
Iterator<String> it = fieldValue.keySet().iterator();
while (it.hasNext()) {
String field = it.next();
String method = getMethodByFieldName(METHODType_SET, field);
Method m = object.getClass().getMethod(method,
new Class[] { String.class });
m.invoke(object, new Object[] { fieldValue.get(field) });
//System.out.print(field);
//System.out.println(fieldValue.get(field));
}
return object; } /**
* 通过字段名得到相应的set,get方法名
*
* @param methodType
* @param fieldName
* @return
*/
private String getMethodByFieldName(int methodType, String fieldName) {
String methodName = "";
if (methodType == METHODType_GET) {
methodName = "get" + Character.toUpperCase(fieldName.charAt(0))
+ fieldName.substring(1);
} else if (methodType == METHODType_SET) {
methodName = "set" + Character.toUpperCase(fieldName.charAt(0))
+ fieldName.substring(1);
}
return methodName;
} /**
* 通过方法名得到相应字段的值
*
* @param methodName
* @return
* @throws Exception
*/
private String getFieldValueByGetMethod(String methodName) {
Method m = null;
String value = "";
try {
m = object.getClass().getMethod(methodName);
value = (String) m.invoke(object); } catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return value;
} /*public String setFieldValueBySetMethod(String methodName, String value) {
Method m;
// String value = "";
try {
m = object.getClass().getMethod(methodName);
value = (String) m.invoke(object, new Object[] { value });
System.out.println("反射 set方法" + value);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return value;
}*/}
如果一个对象解析完成后马上输出的,不把他放到Vector中,就能把五个对象全部输入。一旦放到vector中,结果五个对象全部变成id为5的对象。
只要你能解析出对象,放入Vector中肯定是不会覆盖的,要放入Set中则需要重写equals方法!
楼主要首先确定解析出的5个对象没问题,再看为什么放入容器时出错了
/**
* 变量初试化
*/
Object o = co.newInstance();
/*你的问题就在这里了, 你在循环体外面创建的实例, 循环里面add的o1只是o的引用, 改变的也一直是o里面的值, 最终的vector里面保存的事实上5个都是o,所以出现了你的问题.非常抱歉在一楼没有仔细看就回答了.
事实上应该是 Object o1 = co.newInstance();然后把后面的 o1 = r.setFieldNameAndValue(o, xmlfieldValue);
改成 o1 = r.setFieldNameAndValue(o1, xmlfieldValue);
就行了*/
...
试了下,没用,传入o只是要得到对象o的私有字段。不是用来装对象的。这个类似于hibernate中,HQL中的Query的list方法。
你确定你试了?
//你在你自己的这个方法前打印传入的object,之后打印传出的object就明白了,
//从你的这个方法你应该能发现你一直在对同一个object去setValue
public Object setFieldNameAndValue(Object object,
Map<String, String> fieldValue) throws Exception
{
System.out.println("传入的O : "+object);
// 通过map中的字段得到object中的相应的舌头方法,然后把map中的值设进去返回该对象
Iterator<String> it = fieldValue.keySet().iterator();
while (it.hasNext()) {
String field = it.next();
String method = getMethodByFieldName(METHODType_SET, field);
Method m = object.getClass().getMethod(method,
new Class[] { String.class });
m.invoke(object, new Object[] { fieldValue.get(field) }); // System.out.print(field);
// System.out.println(fieldValue.get(field));
}
System.out.println("传出的O : "+object);
return object; }并且我试过我改的代码了,确认可行
if (oobjectNodeList != null && oobjectNodeList.getLength() > 0) {
for (int i = 0; i < oobjectNodeList.getLength(); i++) {
Node node = oobjectNodeList.item(i);
Object o1 = co.newInstance(); if (node.getNodeType() == Node.ELEMENT_NODE) {
Iterator<String> it = s.iterator();
while (it.hasNext()) {
String field = it.next();
Element e = (Element) node;
NodeList nodeList = e
.getElementsByTagName(field);
String fieldValue = nodeList.item(0)
.getChildNodes().item(0).getNodeValue();
xmlfieldValue.put(field, fieldValue);
}
o1 = r.setFieldNameAndValue(o1, xmlfieldValue);
// 测试是否全部解析,结果全部解析了
System.out.println(o1.toString()); }
objectList.add(o1);
}
// 这个管取第几个对象,结果都是id为5的那个对象
System.out.println(objectList.get(1).toString());
}
/*
输出结果
Linkman [id=1, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]
Linkman [id=2, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]
Linkman [id=3, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]
Linkman [id=4, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]
Linkman [id=5, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]
Linkman [id=2, location=赣州, mail=谷歌邮箱, name=cheng, pic=无, re=无, updateTime=今天]
*/