Here is my code: (source: http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/)
Here is my XML file: removed
On run, the NoResults appears and I get the string “h1” which isn’t a xml tag in my file.
I am not sure if my xml file is being converted incorrectly or I can’t successfully find the “members” tag.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
XMLParser parse = new XMLParser();
String xml = parse.getXmlFromUrl(url); // getting XML
Document doc = parse.getDomElement(xml); // getting DOM element
NodeList nodes = doc.getElementsByTagName(KEY_MEMBER);
// looping through all item nodes <member>
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put(KEY_FNAME, parse.getValue(e, KEY_FNAME));
map.put(KEY_MNAME, parse.getValue(e, KEY_MNAME));
map.put(KEY_LNAME, parse.getValue(e, KEY_LNAME));
map.put(KEY_STATE, parse.getValue(e, KEY_STATE));
map.put(KEY_PARTY, parse.getValue(e, KEY_PARTY));
mylist.add(map);
}
if((nodes.getLength() <= 0)){
Toast.makeText(AndroidXMLParsingActivity.this, "NoResults " + doc.getDocumentElement().getNodeName(), Toast.LENGTH_LONG).show();
finish();
}
Since “h1” is an html tag, i would guess that the url your are hitting is returning html, not xml. an easy way to determine this, of course, would be to print out the xml string before you parse it.
fyi, not sure about the limitations of coding on android, but it’s generally a bad idea to convert an xml document to string before you parse it (for a variety of reasons ranging from “breaking” the xml to needlessly increasing the memory usage).