Reputation: 491
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?
Upvotes: 0
Views: 4166
Reputation: 418
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();
}
}
Upvotes: 1
Reputation: 181
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();
}
Upvotes: 1