xml parsing and show the data in a list view - android

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.

Related

Getting specific Node Value from XML and add to ArrayLIst

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

how use an array in string content android?

i have a xml file and get data from this file and save it in arraylist
xml file :
<head>33</head>
<link>http://hamrahtest.ir/testcenterv13.apk</link>
</group>
<group>
<head>355</head>
<link>http://hamrahtest.ir/testcenterv5555.apk</link>
</group>
protected void onPostExecute(Document doc) {
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
NodeList nl = doc.getElementsByTagName(KEY_GROUPS);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_HEAD, parser.getValue(e, KEY_HEAD));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
// adding HashList to ArrayList
songsList.add(map);
}
Log.d("tagname",songsList.get(1).get(KEY_HEAD).toString());
}
Under this code i have a line :
private static final String[] CONTENT = new String[] {" page1","page2"};
how i can insert KEY_HEAD valuse to CONTENT Instead {" page1","page2"} ?
You can get Set of HashMap keys using Set<String> and the convert Set<String> to String[] using toArray(T[] a)
Set<String> keys = map.keySet();
String[] CONTENT = new String[keys .size()];
keys.toArray(CONTENT);

capitalize first letter of a xml element

I'm a newbie so please be patient.
I have the following code retrieving the nodes and its fine. I've attempted to get the 'status' node to have it's first letter capitalized with little success, it force closes.
What I've done is convert the element to a string. I figured out that I could use the capitalization code for all the elements 'e' but I'd rather use it for status.
Why is it forcing close?
Could someone please help me with this?
NodeList nodes = doc.getElementsByTagName("line");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("id", XMLFunctions.getValue(e, "id"));
map.put("name", XMLFunctions.getValue(e, "name"));
map.put("status", XMLFunctions.getValue(e, "status"));
map.put("message", XMLFunctions.getValue(e, "message"));
mylist.add(map);
//element to string
Document document = e.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document
.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
String str = serializer.writeToString(e);
//capitalization
if (str.length() <= 1) {
str = str.toLowerCase();
} else {
str = str.substring(0, 1).toLowerCase() + str.substring(1);
}
Try this,
str = str.substring(0, 1).toLowerCase().concat(str.substring(1));
I solved this using the following code:
public static String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
char[] chars = XMLFunctions.getElementValue(n.item(0)).toLowerCase().toCharArray();
boolean found = false;
for (int i = 0; i < chars.length; i++) {
if (!found && Character.isLetter(chars[i])) {
chars[i] = Character.toUpperCase(chars[i]);
found = true;
} else if (Character.isWhitespace(chars[i]) || chars[i]=='.' || chars[i]=='\'') { // You can add other chars here
found = false;
}
}
return String.valueOf(chars);

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