如何将节点值设置为 Bean 并添加到 Bean 的 ArrayList [英] How to set nodevalues to Bean and add to ArrayList of Beans

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

问题描述

我正在根据 node 的元素解析 rss 提要并将其设置为 bean 对象.稍后,我需要将其存储在 bean 的 ArrayList 中.看起来我在设置 bean 值并将其添加到 bean 的 ArrayList 时做错了.这是我的代码:

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] 这不是预期的.我需要将实际值存储在 bean 的数组列表中.ArrayList 显示对象而不是存储的实际值.你能更正我的代码吗?

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 不复制存储值,它只存储对添加对象的引用.因此,在您的代码中,您将一个 bean 实例放置了 3 次.

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

如果问题仅在于打印项目中的重复值,您应该更改代码以在每次循环迭代时创建新的 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)
}

但是如果您在打印 bean 字段时遇到问题,您应该重写 RssBean 类中的 toString() 方法

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();
}

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

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