问题描述:
当读取完xml文件时,objectvector中理论是装的应该是5个不同的对象(id为1,2,3,4,5),但是结果输出的五个对象都是相同(id为5的对象)
/**
*读取xml文件
* @param fileName xml文件地址
* @param co 要解析成为的对象类型
* @return
*/
public Vector<?> read(String fileName, Class<?> co) { /**
* 变量声明
*/
// 从xml文档中解析出来的字段及其值
Map<String, String> xmlfieldValue = new HashMap<String, String>();
// 对象字段的集合,用于从xml中取该字段的值
Set<String> s = new HashSet<String>();
//装解析的对象
Vector<Object> objectList = new Vector<Object>();
//类名
String className = "";
try {
/**
* 变量初试化
*/
Object o = co.newInstance();
className = o.getClass().getSimpleName();
//自定义的反射的工具类
Reflect r = new Reflect(o);
s = r.getFieldName(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File(fileName);
if (file.exists()) {
Document doc = db.parse(file);
Element docEle = doc.getDocumentElement();
NodeList oobjectNodeList = docEle.getElementsByTagName(className);
if (oobjectNodeList != null && oobjectNodeList.getLength() > 0)
{
for (int i = 0; i < oobjectNodeList.getLength(); i++)
{
Node node = oobjectNodeList.item(i);
Object o1 = new Object();
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(o, xmlfieldValue);
//这里能全部输出五个不同的对象
System.out.println("内部一 "+o1.toString());
objectList.add(o1);
}
}
//这里不管取第几个对象,结果都是id为5的对象
System.out.println("内部二 "+objectList.get(1).toString());
}
}
} catch (Exception e) {
System.out.println(e);
}
return objectList;
}
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 class Linkman {
private String id ;
private String name;
private String location;
private String mail;
private String pic;
private String re;
private String updateTime;
public Linkman(){} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getLocation() {
return location;
} public void setLocation(String location) {
this.location = location;
} public String getMail() {
return mail;
} public void setMail(String mail) {
this.mail = mail;
} public String getPic() {
return pic;
} public void setPic(String pic) {
this.pic = pic;
} public String getRe() {
return re;
} public void setRe(String re) {
this.re = re;
} public String getUpdateTime() {
return updateTime;
} public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
} @Override
public String toString() {
return "Linkman [id=" + id + ", location=" + location + ", mail="
+ mail + ", name=" + name + ", pic="
+ pic + ", re=" + re + ", updateTime=" + updateTime
+ "]";
}要读取的xml文件的内容:
<?xml version="1.0" encoding="GB2312" standalone="no" ?>
- <usr>
- <Linkman>
<id>1</id>
<mail>谷歌邮箱</mail>
<location>赣州</location>
<re>无</re>
<updateTime>今天</updateTime>
<name>cheng</name>
<pic>无</pic>
</Linkman>
- <Linkman>
<id>2</id>
<mail>谷歌邮箱</mail>
<location>赣州</location>
<re>无</re>
<updateTime>今天</updateTime>
<name>cheng</name>
<pic>无</pic>
</Linkman>
- <Linkman>
<id>3</id>
<mail>谷歌邮箱</mail>
<location>赣州</location>
<re>无</re>
<updateTime>今天</updateTime>
<name>cheng</name>
<pic>无</pic>
</Linkman>
- <Linkman>
<id>4</id>
<mail>谷歌邮箱</mail>
<location>赣州</location>
<re>无</re>
<updateTime>今天</updateTime>
<name>cheng</name>
<pic>无</pic>
</Linkman>
- <Linkman>
<id>5</id>
<mail>谷歌邮箱</mail>
<location>赣州</location>
<re>无</re>
<updateTime>今天</updateTime>
<name>cheng</name>
<pic>无</pic>
</Linkman>
</usr>
当读取完xml文件时,objectvector中理论是装的应该是5个不同的对象(id为1,2,3,4,5),但是结果输出的五个对象都是相同(id为5的对象)
/**
*读取xml文件
* @param fileName xml文件地址
* @param co 要解析成为的对象类型
* @return
*/
public Vector<?> read(String fileName, Class<?> co) { /**
* 变量声明
*/
// 从xml文档中解析出来的字段及其值
Map<String, String> xmlfieldValue = new HashMap<String, String>();
// 对象字段的集合,用于从xml中取该字段的值
Set<String> s = new HashSet<String>();
//装解析的对象
Vector<Object> objectList = new Vector<Object>();
//类名
String className = "";
try {
/**
* 变量初试化
*/
Object o = co.newInstance();
className = o.getClass().getSimpleName();
//自定义的反射的工具类
Reflect r = new Reflect(o);
s = r.getFieldName(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File(fileName);
if (file.exists()) {
Document doc = db.parse(file);
Element docEle = doc.getDocumentElement();
NodeList oobjectNodeList = docEle.getElementsByTagName(className);
if (oobjectNodeList != null && oobjectNodeList.getLength() > 0)
{
for (int i = 0; i < oobjectNodeList.getLength(); i++)
{
Node node = oobjectNodeList.item(i);
Object o1 = new Object();
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(o, xmlfieldValue);
//这里能全部输出五个不同的对象
System.out.println("内部一 "+o1.toString());
objectList.add(o1);
}
}
//这里不管取第几个对象,结果都是id为5的对象
System.out.println("内部二 "+objectList.get(1).toString());
}
}
} catch (Exception e) {
System.out.println(e);
}
return objectList;
}
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 class Linkman {
private String id ;
private String name;
private String location;
private String mail;
private String pic;
private String re;
private String updateTime;
public Linkman(){} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getLocation() {
return location;
} public void setLocation(String location) {
this.location = location;
} public String getMail() {
return mail;
} public void setMail(String mail) {
this.mail = mail;
} public String getPic() {
return pic;
} public void setPic(String pic) {
this.pic = pic;
} public String getRe() {
return re;
} public void setRe(String re) {
this.re = re;
} public String getUpdateTime() {
return updateTime;
} public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
} @Override
public String toString() {
return "Linkman [id=" + id + ", location=" + location + ", mail="
+ mail + ", name=" + name + ", pic="
+ pic + ", re=" + re + ", updateTime=" + updateTime
+ "]";
}要读取的xml文件的内容:
<?xml version="1.0" encoding="GB2312" standalone="no" ?>
- <usr>
- <Linkman>
<id>1</id>
<mail>谷歌邮箱</mail>
<location>赣州</location>
<re>无</re>
<updateTime>今天</updateTime>
<name>cheng</name>
<pic>无</pic>
</Linkman>
- <Linkman>
<id>2</id>
<mail>谷歌邮箱</mail>
<location>赣州</location>
<re>无</re>
<updateTime>今天</updateTime>
<name>cheng</name>
<pic>无</pic>
</Linkman>
- <Linkman>
<id>3</id>
<mail>谷歌邮箱</mail>
<location>赣州</location>
<re>无</re>
<updateTime>今天</updateTime>
<name>cheng</name>
<pic>无</pic>
</Linkman>
- <Linkman>
<id>4</id>
<mail>谷歌邮箱</mail>
<location>赣州</location>
<re>无</re>
<updateTime>今天</updateTime>
<name>cheng</name>
<pic>无</pic>
</Linkman>
- <Linkman>
<id>5</id>
<mail>谷歌邮箱</mail>
<location>赣州</location>
<re>无</re>
<updateTime>今天</updateTime>
<name>cheng</name>
<pic>无</pic>
</Linkman>
</usr>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货