面向对象读取xml文件的内容是出现问题

问题描述:当读取完xml文件时,objectvector中理论是装的应该是5个不同的对象(id为1,2,3,4,5),但是结果输出的五个对象都是相同(id为5的对象)

[code="java"]
/**
*
* @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> objectvector = 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 =null;
                    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(0).toString());
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }
    return objectList;
}

[/code]

[code="java"]
public class Linkman {
private String id ;
private String name;
private String location;
private String mail;
private String pic;
private String remark;
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 getRemark() {
    return remark;
}

public void setRemark(String remark) {
    this.remark = remark;
}

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 + ", remark=" + remark + ", updateTime=" + updateTime
            + "]";
}

[/code]

[code="xml"]
<?xml version="1.0" encoding="GB2312" standalone="no" ?>

  • 1谷歌邮箱赣州无今天cheng无
  • 2谷歌邮箱赣州无今天cheng无
  • 3谷歌邮箱赣州无今天cheng无
  • 4谷歌邮箱赣州无今天cheng无
  • 5谷歌邮箱赣州无今天cheng无

[/code]

1个回答

objectList在哪里定义的?
怎么定义的?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐