How to set nodevalues to Bean and add to ArrayList of Beans

我的未来我决定 提交于 2019-12-13 03:24:41

问题


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 "?";
}

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?


回答1:


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

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

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



回答2:


You are adding the object in the list that's why it is printing the object "RssBean@450cceb3".

The values are stored in Objects and you can print it by override the toString() method in your RssBean class.

Update your RssBean like:

public class RssBean {

    String title;
    String description;
    String value;

    public RssBean(String title, String description, String value) {
        this.title = title;
        this.description = description;
        this.value = value;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();

        result.append("Title: " + title);
        result.append("Description: " + description);
        result.append("Value: " + value);


        return result.toString();
    }
}


来源:https://stackoverflow.com/questions/18583487/how-to-set-nodevalues-to-bean-and-add-to-arraylist-of-beans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!