Created
December 5, 2013 18:38
-
-
Save rachitmishra/7810837 to your computer and use it in GitHub Desktop.
Parsing XML on android using XML pull parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context=".MainActivity" > | |
<Button | |
android:id="@+id/button1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:layout_centerHorizontal="true" | |
android:layout_marginBottom="90dp" | |
android:text="Parse" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package in.xmlparser; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import org.xmlpull.v1.XmlPullParser; | |
import org.xmlpull.v1.XmlPullParserException; | |
import android.app.Activity; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Button button = (Button) findViewById(R.id.button1); | |
button.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View arg0) { | |
new AsyncTask<Void, Void, Void>() { | |
@Override | |
protected Void doInBackground(Void... arg0) { | |
readXmlFile("code.xml"); | |
return null; | |
} | |
}.execute(); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
public InputStream readFile(String fileName) throws FileNotFoundException, | |
IOException { | |
return new FileInputStream(Environment.getExternalStorageDirectory() | |
+ "/" + fileName); | |
} | |
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(); | |
} | |
} | |
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..."); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package in.xmlparser; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import org.xmlpull.v1.XmlPullParser; | |
import org.xmlpull.v1.XmlPullParserException; | |
import android.util.Xml; | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment