Hi guys, If we have to parse the different type of XML response then this will be some how tedious task because we will have to create separate parser for all types of XML files. Today I saw one question for common class for xml parsing so I have create one class to parse all type of XML response.
Below I am putting my common XML parser class
XMLObject.java
XmlParser.java
CommonXMLParserActivity.java
Note:: This is for demo and it may have some problem. If you have any issue or if you found anything wrong in this demo, please suggest me to make it better. Thanks.
Below I am putting my common XML parser class
XMLObject.java
public class XMLObject {
HashMap<String, String> params;
List<XMLObject> childs;
String value;
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public HashMap<String, String> getParams() {
return params;
}
public void setParams(HashMap<String, String> params) {
this.params = params;
}
public List<XMLObject> getChilds() {
return childs;
}
public void setChilds(List<XMLObject> childs) {
this.childs = childs;
}
}
XmlParser.java
public class XmlParser {
public static XMLObject parseXml(InputStream is) throws Exception{
if(is == null) throw new IOException("Inputstream is null from the server");
String key = null;
String value = null;
/**
* Stack is use to know the hierarchy of the XML file
* This will push all the tags when tag starts and pop when tag will end.
*/
Stack<String> mStack = null;
/**
* This hashmap is for add the XMLObject.
*/
HashMap<String, XMLObject> temp = new HashMap<String, XMLObject>();
XMLObject mXmlObject = null;
XmlPullParserFactory mXmlPullParserFactory = XmlPullParserFactory.newInstance();
mXmlPullParserFactory.setNamespaceAware(true);
XmlPullParser xpp = mXmlPullParserFactory.newPullParser();
xpp.setInput(new InputStreamReader(is));
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
mStack = new Stack<String>();
}else if(eventType == XmlPullParser.START_TAG) {
key = xpp.getName();
mXmlObject = new XMLObject();
mXmlObject.setName(key);
mStack.push(key);
int attrCount = xpp.getAttributeCount();
if(attrCount > 0) {
HashMap<String, String> attribs = new HashMap<String, String>(attrCount);
for(int i=0;i<attrCount;i++) {
attribs.put(xpp.getAttributeName(i), xpp.getAttributeValue(i));
} mXmlObject.setParams(attribs);
}
/**
* Here we are temporary adding a current object in the map.
* we will get this object back when this tag will end.
*/
temp.put(key, mXmlObject);
}else if(eventType == XmlPullParser.END_TAG) {
key = xpp.getName();
/**
* We are removing this current tab because it is now closed
*/
String current = mStack.pop();
/**
* Size of the stack will be 0 when we current tag will be last tag of xml.
*/
if(mStack.size() == 0) {
/**
* Here we are fetching the XMLObject which we had set when current tag was started.
* Here we are fetching the current object and updating the value in it.
*/
XMLObject mXmlObject2 = temp.get(key);
mXmlObject2.setValue(value);
temp.put(current, mXmlObject2);
mXmlObject = mXmlObject2; }else {
String parent = mStack.get(mStack.size()-1);
/**
* Here we are fetching the parent tag and updating the parent object to add the current child.
*/
if(temp.containsKey(key)) {
XMLObject mXmlObject2 = temp.get(key);
temp.remove(mXmlObject2);
mXmlObject2.setValue(value);
XMLObject parentObj = temp.get(parent);
List<XMLObject> list = parentObj.getChilds();
if(list == null) list = new ArrayList<XMLObject>();
list.add(mXmlObject2);
parentObj.setChilds(list);
temp.put(parent, parentObj);
} }
value = null;
}else if(eventType == XmlPullParser.TEXT) {
value = xpp.getText();
}
eventType = xpp.next();
}
return mXmlObject;
}
}
CommonXMLParserActivity.java
public class CommonXMLParserActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
InputStream is = getAssets().open("response.xml");
XMLObject mObject = XmlParser.parseXml(is);
if(mObject != null) {
display(mObject);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void display(XMLObject mObject) {
if(mObject != null) {
HashMap<String, String> atts = mObject.getParams();
if(atts != null && atts.size() > 0) {
for (String s : atts.keySet()) {
Log.d("KEY:" + mObject.getName() + " :: Attribute: ", s + "=" + atts.get(s));
} }
Log.d("KEY:" + mObject.getName() + " :: Value=", mObject.getValue());
List<XMLObject> mXmlObjects = mObject.getChilds();
if(mXmlObjects != null && mXmlObjects.size() > 0) {
for (XMLObject xmlObject : mXmlObjects) {
display(xmlObject);
} }
}
}
}
response.xml(put this file in assets folder)
<?xml version="1.0" encoding="utf-8"?>
<root status="1">
<persons>
<person id="1">
<name>DBP</name>
<phone>1234566789</phone>
</person>
<person id="2">
<name>HVS</name>
<phone>987654321</phone>
</person>
<person id="3">
<name>RBH</name>
<phone>234346664</phone>
</person>
<person id="4">
<name>LP</name>
<phone>5675677765</phone>
</person>
</persons>
</root>
Note:: This is for demo and it may have some problem. If you have any issue or if you found anything wrong in this demo, please suggest me to make it better. Thanks.
how can i show my output on emulator???
ReplyDeletenice example i used this code on my project. thanks for sharing. http://androidostutor.net/how-to-parsing-xml-file-in-android/
ReplyDelete