# While you have some errors in you XML code, which you may check from above answers, the ideal way to parse your XML code will be something
like
Ex. XML file content (saved in external storage as code.xml)
<?xml version="1.0" encoding="utf-8"?>
<countries>
<country>
<name>Iran</name>
<phonecode>+98</phonecode>
<code>IRI</code>
</country>
<country>
<name>United State</name>
<phonecode>+1</phonecode>
<code>USA</code>
</country>
</countries>
# Lets begin parsing it
1. First create a Helper class
package in.example.helpers;
import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
public class XmlDataParseHelper {
private XmlPullParser parser;
private static final String NULL = null;
/**
*
* @param in
* @throws XmlPullParserException
* @throws IOException
* @throws IllegalArgumentException
*/
public XmlDataParseHelper(InputStream in) throws XmlPullParserException,
IOException, IllegalArgumentException {
try {
parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @return XmlPullParser
*/
public XmlPullParser getParser() {
return parser;
}
/**
*
* @param parser
* @param tag
* @return String
* @throws IOException
* @throws XmlPullParserException
*/
public static String readTag(XmlPullParser parser, String tag)
throws IOException, XmlPullParserException {
String tagData = "";
parser.require(XmlPullParser.START_TAG, NULL, tag);
if (parser.next() == XmlPullParser.TEXT) {
tagData = parser.getText();
parser.nextTag();
}
parser.require(XmlPullParser.END_TAG, NULL, tag);
return tagData;
}
/**
*
* @param parser
* @param tag
* @param attributeName
* @return String
* @throws IOException
* @throws XmlPullParserException
*/
public static String readAttribute(XmlPullParser parser, String tag,
String attributeName) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, NULL, tag);
String attributeData = parser.getAttributeValue(NULL, attributeName);
parser.nextTag();
parser.require(XmlPullParser.END_TAG, NULL, tag);
return attributeData;
}
}
2. Read the xml file as
public InputStream readFile(String fileName) throws FileNotFoundException,
IOException {
//check external storage present
// else throw new IOException();
return new FileInputStream(Environment.getExternalStorageDirectory()
+ "/" + fileName);
}
3. Pass the InputStream to another method which initializes the XMLPullParser object
public void readXmlFile(String fileName) {
try {
if (fileName.isEmpty())
throw new NullPointerException();
readData(new XmlDataParseHelper(readFile(fileName)).getParser());
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
4. Atlast parse the XMLPullParser object as
public void readData(XmlPullParser parser)
throws XmlPullParserException, IOException {
int eventType = parser.getEventType();
String tagName = "";
Log.w("Developer", "Reading file...");
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_DOCUMENT :
Log.w("Developer", "Reading backup file...");
break;
case XmlPullParser.START_TAG :
tagName = parser.getName();
if (tagName.equals("countries")) {
Log.w("XMLParse", "Start TAG countries");
// do something when countries tag starts
}
if (tagName.equals("country")) {
Log.w("XMLParse", "Start TAG country");
// do some when country tag starts
} else if (tagName.equals("name")) {
// read tag value using XmlDataParseHelper class
String countryName = XmlDataParseHelper.readTag(parser,
"name");
Log.w("XmlParser", "Country name : "+countryName);
} else if (tagName.equals("phonecode")) {
String countryPhoneCode =
XmlDataParseHelper.readTag(parser,"phonecode");
Log.w("XmlParser", "Country Phone code : "+countryPhoneCode);
} else if (tagName.equals("code")) {
String countryCode =
XmlDataParseHelper.readTag(parser, "code");
Log.w("XmlParser", "Country code name : "+countryCode);
}
break;
case XmlPullParser.END_TAG :
tagName = parser.getName();
if (tagName.equals("countries")) {
// do something when counties tag is close.
}
break;
}
eventType = parser.next();
}
Log.w("Developer", "File parsing complete...");
}
To read a file just call the method readXmlFile(String fileName) in an AsyncTask with the name of file stored in the External Storage.
#I hope it helps you and others in the future.
The whole android working example code is here https://gist.github.com/rachitrm/7810837 or fork the project http://www.github.com/rachitrm/rm-xmlparser/