Android Easy XML Parsing

Use this class to make easy to XML parsing : XMLPARSE

Reference Android : DirectTutorial

xmlparse class ==>
import android.util.Log;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class XMLParser {

// constructor
public XMLParser() {

}

String TAG = this.getClass().getName();

public String getXmlFromFolder(InputStream is) {
String xml = null;

BufferedReader in = null;
try {
StringBuilder buf = new StringBuilder();
// InputStream is = context.getAssets().open(name);
in = new BufferedReader(new InputStreamReader(is));

String str;
boolean isFirst = true;
while ((str = in.readLine()) != null) {
if (isFirst)
isFirst = false;
else
buf.append('n');
buf.append(str);
}
return buf.toString();
} catch (IOException e) {
Log.e(TAG, "Error opening asset " + e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e(TAG, "Error closing asset " + e);
}
}
}

return null;
}


/**
* Getting XML from URL making HTTP request
*
* @param url string
*/
// used this on background task
public String getXmlFromUrl(String url) {
String xml = null;

try {
// defaultHttpClient
Log.i("URL", "" + url);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}

public Document getDomElement(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {

DocumentBuilder db = dbf.newDocumentBuilder();

InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);

} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}

return doc;
}

/**
* Getting node value
*
* @param elem element
*/
public final String getElementValue(Node elem) {
Node child;
if (elem != null) {
if (elem.hasChildNodes()) {
for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
}
}
}
return "";
}

public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}

public String getAttributeValue(Element item, String str) {
return item.getAttribute(str);
}

}

How To use this CLASS :








// All static variables


// XML node keys

static final String KEY_ITEM = "item"; // parent node

static final String KEY_NAME = "name";

static final String KEY_COST = "cost";

static final String KEY_DESC = "description";


XMLParser parser = new XMLParser();

String xml = parser.getXmlFromUrl(URL); // getting XML

Document doc = parser.getDomElement(xml); // getting DOM element


NodeList nl = doc.getElementsByTagName(KEY_ITEM);


// looping through all item nodes <item>     

for (int i = 0; i < nl.getLength(); i++) {

    String name = parser.getValue(e, KEY_NAME); // name child value

    String cost = parser.getValue(e, KEY_COST); // cost child value

    String description = parser.getValue(e, KEY_DESC); // description child value

}


 

Get XML from Folder :
InputStream is = getResources().getAssets().open("test.xml"); //we passout this inputstream only

Contributer

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments:

Hot-product

Best Watch Store

Popular Posts

Contact Form

Name

Email *

Message *