如何nodevalues​​设置为Bean并增加豆的ArrayList [英] How to set nodevalues to Bean and add to ArrayList of Beans

查看:96
本文介绍了如何nodevalues​​设置为Bean并增加豆的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解析基于节点的元素的RSS提要并将其设置为一个bean对象。后来,我需要这些信息存储在豆类的ArrayList。貌似我在设定值豆,也将它添加到豆子的ArrayList做错了。
这里是我的code:

I am parsing a rss feed based on the elements of node and set it to a bean object. Later, I need to store this in an ArrayList of beans. Looks like I am doing wrong at setting the bean value and also adding it to ArrayList of beans. Here is my code:

public static void main(String arg[]) {
  ArrayList<RssBean> list = new ArrayList<RssBean>(); 
  RssBean bean = new RssBean(null, null, null); 

 String xmlRecords =
  "<rss>" +"<channel>"+
  " <item>" +
  "   <title>John</title>" + 
  "   <description> Desc1 </description>"+
  " </item>" +
  " <item>" +
  "   <title>Sara</title>" + 
  "   <description> Desc2 </description>"+
  " </item>" + "</channel>"+
  "</rss>";

try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlRecords));

    Document doc = db.parse(is);
    NodeList nodes = doc.getElementsByTagName("item");

    // iterate the  items
    for (int i = 0; i < nodes.getLength(); i++) {
       Element element = (Element) nodes.item(i);

       NodeList title = element.getElementsByTagName("title");
       Element line = (Element) title.item(0);  
       bean.setTitle(getCharacterDataFromElement(line));

       NodeList description = element.getElementsByTagName("description");
       line = (Element) description.item(0); 
       bean.setDescription(getCharacterDataFromElement(line));

       list.add(bean);           
    } 

    //print list values
    ListIterator<RssBean> iter = list.listIterator() ;
    while(iter.hasNext()) 
    {
        RssBean result = (RssBean)iter.next();
        System.out.println(result);
    }

}
catch (Exception e) {
    e.printStackTrace();
} 
}

public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
   CharacterData cd = (CharacterData) child;
   return cd.getData();
}
return "?";
}

当我打印出列表中的值,我得到 - 列表值==> [RssBean @ 450cceb3,RssBean @ 450cceb3]这是不expected.I需要的实际值存储在豆类的数组列表。 ArrayList的显示对象,而不是存储的实际值。能否请您纠正我的code?

When I am printing out list values I get - List Values ======>[RssBean@450cceb3, RssBean@450cceb3] which is not expected.I need to store the actual values in the array list of beans. ArrayList displays objects rather than the actual value stored. Can you please correct my code?

推荐答案

ArrayList的不复制存储的价值,IT卖场只是参考,以添加的对象。所以,在你的code你把一只bean实例三次。

ArrayList doesn't copy storing value, it stores just reference to added object. So, in your code you put one bean instance three times.

如果只在印刷复制物品的价值问题,你应该改变你的code来创建新的bean实例每次循环

If the problem only in duplicating values in printed items, you should change your code to create new bean instance every loop iteration

for (int i = 0; i < nodes.getLength(); i++) {
  RssBean bean = new RssBean(null, null, null);
  Element element = (Element) nodes.item(i);
  // ...
  list.add(bean)
}

但是,如果你在打印豆领域的问题,您应该重写的toString()的RssBean类方法

But if your the problem in printing bean fields, you should override toString() method in RssBean class

@Override
public String toString() {
    return "Bean Values ======>" + getTitle + "&&" + getDesc();
}

这篇关于如何nodevalues​​设置为Bean并增加豆的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆