Getting specific Node Value from XML and add to ArrayLIst - android

I have an XML like this
<reg>
<user>
<Name> abc</Name>
<Email> abc#xyz.com</Email>
<picture> sdCard/imges.145.jpg </Picture>
<Date> 12/12/2012</Date>
</user>
<user>
<Name> abc dfg</Name>
<Email> ertdg#xyz.com</Email>
<picture> sdCard/imges.145.jpg </Picture>
<Date> 23/12/2013</Date>
</user>
</reg>
i want to get values of specific nodes and add them to ArrayList to show the in ListView
I have tried this code but it only gives values for Last Record
Users setUser = new Users();
final ArrayList<Users> uData = new ArrayList<Users>();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/reg/user/Name";
System.out.println(expression);
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getTextContent());
setUser.setName(nodeList.item(i).getTextContent());
}
String expression1 = "/reg/user/Email";
System.out.println(expression1);
NodeList nodeList1 = (NodeList) xPath.compile(expression1).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList1.getLength(); i++) {
System.out.println(nodeList1.item(i).getTextContent());
setUser.setEmail(nodeList1.item(i).getTextContent());
}
String expression2 = "/reg/user/Picture";
System.out.println(expression2);
NodeList nodeList2 = (NodeList) xPath.compile(expression2).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList2.getLength(); i++) {
System.out.println(nodeList2.item(i).getTextContent());
setUser.setimage(nodeList2.item(i).getTextContent());
}
String expression3 = "/reg/user/LastEdited";
System.out.println(expression3);
NodeList nodeList3 = (NodeList) xPath.compile(expression3).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList3.getLength(); i++) {
System.out.println(nodeList3.item(i).getTextContent());
setUser.setDate(nodeList3.item(i).getTextContent());
} uData.add(setUser);
How can i make this to show all records ?

The reason why your code only gives values for Last Record already explained by #playmaker420 in comment. To fix it, you can try to loop through <user> elements instead of looping through each <name>, <email>, <picture>, and <date> separately :
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/reg/user";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Users setUser = new Users();
Element el = (Element) nodeList.item(i);
String name = el.getElementsByTagName("Name").item(0).getTextContent();
setUser.setName(name);
String email = el.getElementsByTagName("Email").item(0).getTextContent();
setUser.setEmail(email);
String picture = el.getElementsByTagName("picture").item(0).getTextContent();
setUser.setimage(picture);
String date = el.getElementsByTagName("Date").item(0).getTextContent();
setUser.setDate(date);
uData.add(setUser);
}

Related

xml parsing and show the data in a list view

I have a xml file like [this][1] and i need to get the all the "edition name" according to the "group name"
I have to create a list of group name and show the list of editions name on clicking of the group name.
OutPut :
onclick of catalogs :
this xml is present in different languages, is there any way to get all the editions name without comparing with group name.
currently i am using type tag to get all the catalogs
:
if(parser.getValue(ele2, "type").toString().equalsIgnoreCase("catalog"))
But some magazines are also present in catalogs type. so not getting correct results
..
How to get all the editions name where group name is catalogs and so on ...
Or a better way to do so.. I am not sure the code I am using is a good approach.
Kindly suggest.
My code :
final String URL = "http://xml/web/?frg="+lang+"&user="+count+"_"+user;
System.out.println("URL child : " + URL);
parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
doc = parser.getDomElement(xml); // getting DOM element
NodeList n1 = doc.getElementsByTagName("groups");
for (int i = 0; i < 1; i++) { //ToDo : worked temp
Element e = (Element) n1.item(0);
NodeList children = e.getChildNodes();
if(item_name.equalsIgnoreCase("Catalogs")||item_name.equalsIgnoreCase("Kataloge")) // there could be other languages
{
NodeList nodeList2 = doc.getElementsByTagName("edition");
for (int k = 0; k < nodeList2.getLength(); k++) {
HashMap<String, String> map = new HashMap<String, String>();
Element ele2 = (Element) nodeList2.item(k);
if(parser.getValue(ele2, "type").toString().equalsIgnoreCase("catalog"))
{
System.out.println(""+parser.getValue(ele2, "name").toString());
System.out.println(""+parser.getValue(ele2, "cat_url").toString());
map.put(ss2, parser.getValue(ele2,ss2 ));
map.put(KEY_THUMB_URL, parser.getValue(ele2, KEY_THUMB_URL));
map.put(CAT_URL, parser.getValue(ele2,"cat_url"));
// adding HashList to ArrayList
songsList.add(map);
}
}
}
if(item_name.equalsIgnoreCase("Magazines")||item_name.equalsIgnoreCase("Magazine"))
{
NodeList nodeList2 = doc.getElementsByTagName("edition");
for (int k = 0; k < nodeList2.getLength(); k++) {
HashMap<String, String> map = new HashMap<String, String>();
Element ele2 = (Element) nodeList2.item(k);
if(parser.getValue(ele2, "type").toString().equalsIgnoreCase("pdf"))
{
System.out.println(""+parser.getValue(ele2, "name").toString());
System.out.println(""+parser.getValue(ele2, "cat_url").toString());
map.put(ss2, parser.getValue(ele2,ss2 ));
map.put(KEY_THUMB_URL, parser.getValue(ele2, KEY_THUMB_URL));
map.put(CAT_URL, parser.getValue(ele2,"cat_url"));
// adding HashList to ArrayList
songsList.add(map);
}
}
}
if(item_name.equalsIgnoreCase("Instructions"))
{
NodeList n2 = doc.getElementsByTagName("groups");
for (int i1 = 0; i1 < 1; i1++) { //ToDo : worked temp
Element e1 = (Element) n2.item(0);
NodeList children1 = e1.getChildNodes();
for (int j = 0; j <children1.getLength(); j++) {
HashMap<String, String> map = new HashMap<String, String>();
child = children1.item(j);
// System.out.println("name node22 inside " + parser.getValue((Element)child, "name"));
if(parser.getValue((Element)child, "name").equals("Instructions"))
{
if (child.getNodeName().equalsIgnoreCase("group")) {
NodeList nl2 = child.getChildNodes();
System.out.println("Nodelist lenght : " + nl2.getLength());
for (int jj = 0; jj <nl2.getLength(); jj++) {
Node nn =nl2.item(jj);
System.out.println("name node22 inside " + parser.getValue((Element)nn, "name"));
}
// map.put(KEY_TITLE, parser.getValue((Element)child,KEY_TITLE ));
//map.put(KEY_THUMB_URL, parser.getValue((Element)child, KEY_THUMB_URL));
// adding HashList to ArrayList
//songsList.add(map);
}
}
}
}
}
Solved :
Changed this :
Element e = (Element) n1.item(0);
NodeList nodeList2 = doc.getElementsByTagName("edition");
to this :
Element e = (Element) n1.item(0);
NodeList nodeList2 = e.getElementsByTagName("edition");
I'd rather suggest you to build your own parser using the pattern matching approach. Coz it gives you the power to customize things according to your need.
Gson like parser will be a good fit.

Android Parse xml with DOM - same named tags

I have an rss feed where, in every item tag there are two tags named category. I want to get the value of the first one,but unfortunately i get the second one value. This is my code:
// Create required instances
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Parse the xml
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
// Get all <item> tags.
NodeList nl = doc.getElementsByTagName("item");
int length = nl.getLength();
// to length einai posa nea tha emfanisei.Edw tou lew ola pou
// vriskei
for (int i = 0; i < length; i++) {
Node currentNode = nl.item(i);
RSSItem _item = new RSSItem();
NodeList nchild = currentNode.getChildNodes();
int clength = nchild.getLength();
// Get the required elements from each Item
for (int j = 0; j < clength; j = j + 1) {
Node thisNode = nchild.item(j);
String theString = null;
if (thisNode != null && thisNode.getFirstChild() != null) {
theString = thisNode.getFirstChild().getNodeValue();
}
if (theString != null) {
String nodeName = thisNode.getNodeName();
...
....
...
if ("category".equals(nodeName)) {
/*
* NodeList nlList = nl.item(0).getChildNodes();
* Node nValue2 = (Node) nlList.item(0);
* _item.setCategory(nValue2.getNodeValue());
*/
_item.setCategory(theString);
}
}
}
EDIT:
My RSS feed is like:
<item>
<title>my title</title>
<category>Sports</category>
<category>World</category>
</item>
<item>
<title>my title 2</title>
<category>News</category>
<category>Showbiz</category>
</item>
...etc
if you want to get first category from current item Nodes then use Element.getElementsByTagName("category") for getting all category nodes in NodeList and after that use NodeList.item(0) to get first category Element from NodeList do it as:
Element element = (Element) currentNode;
NodeList nodelist = element.getElementsByTagName("category");
Element element1 = (Element) nodelist.item(0);
NodeList category = element1.getChildNodes();
System.out.print("category : " + (category.item(0)).getNodeValue());

how to parse the group of tags from xml in android?

Folks,
this is my xml response:
<parent>
<child>
<name>
<age>
</child>
<child>
<name>
<age>
</child>
...</parent>
i need to parse the each child content as xml string that:
Output Needed:
<child>
<name>
<age>
</child>
i have used normalize method also. but still i am facing problem to get a proper output.
my code is:
NodeList nodes = document.getElementsByTagName("child");// here documentobject is the Document of my xml.
for (int i = 0; i < nodes.getLength(); i++) {
Node element = nodes.item(i);
// nodes.item(i).normalize();
System.out.println(nodes.item(i).getNodeValue());
}
Any Idea?
To do this, you want to use getTextContent() not getNodeValue()....
NodeList nodes = doc.getElementsByTagName("child");
for (int i = 0; i < nodes.getLength(); i++) {
if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
NodeList msgChildren = nodes.item(i).getChildNodes();
for (int j = 0; j < msgChildren.getLength(); j++) {
if (msgChildren.item(j).getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) msgChildren.item(j);
System.out.println("<"+e.getNodeName()+">");
System.out.println(e.getTextContent());
System.out.println("</"+e.getNodeName()+">");
}
}
}
}
Check this below Snippet :
NodeList items_records = element.getElementsByTagName("child");
for (int a = 0; a < items_records.getLength(); a++) {
Node item_records = items_records.item(a);
NodeList properties_records = item_records.getChildNodes();
for (int b = 0; b < properties_records.getLength(); b++) {
Node property_records = properties_records.item(b);
String name_records = property_records.getNodeName();
try{
if (name_records.equalsIgnoreCase("name")) {
parsed_total_records_value = property_records.getFirstChild().getNodeValue();
Log.v("parsed_total_records_value",parsed_total_records_value);
}
}catch(Exception e){
}
}
}

Problem in XML parsing using DOM

I'm parsing an xml content from web service,it creates a problem when parsing the content with
Brown Men’sHockey “Grows for Movember”,when I parse like this by using DOM parses it results Brown Men after the hexadecimal code,the parse is not accepting remaining characters,here the code to parse
NodeList items = elt.getElementsByTagName(ITEM);
for (int i = 0; i < items.getLength(); i++) {
Message_desc message = new Message_desc();
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase(DESCRIPTION)) {
message.setDescription(property.getFirstChild().getNodeValue());
.....}
what I need to do here,please help me.
NodeList nodeList = doc.getElementsByTagName("item");
description = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
description[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList dataList = fstElmnt.getElementsByTagName("description");
Element dataElement = (Element) dataList.item(0);
dataList = dataElement.getChildNodes();
description[i].setText( ((Node) dataList.item(0)).getNodeValue());

Dom parsing in android

i have parsed xml using dom parser,but when the xml element contains no value it throws null pointer Exception.how to check this?...
here my code
NodeList TrackName1 = fstElmnt2.getElementsByTagName("Artist_Name");
Element TrackElement1 = (Element) TrackName1.item(0);
TrackName1 = TrackElement1.getChildNodes();
result=result+((Node) TrackName1.item(0)).getNodeValue()
String Artistname=((Node)TrackName1.item(0)).getNodeValue();
}
Just check for null before you try to use the value?
Object value = ((Node) TrackName1.item(0)).getNodeValue();
if (value != null) {
result = result + value;
}
NodeList nodeList = doc.getElementsByTagName("item");
description = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
description[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList dataList = fstElmnt.getElementsByTagName("description");
Element dataElement = (Element) dataList.item(0);
dataList = dataElement.getChildNodes();
description[i].setText( ((Node) dataList.item(0)).getNodeValue());

Categories

Resources