I am new to this parse using SAX. Ive manage to use this code to get the returned xml and bind it on my getter setter.
My Code:
try {
/**
* Create a new instance of the SAX parser
**/
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL(""); // URL of the XML
/**
* Create the Handler to handle each of the XML tags.
**/
XMLHandler myXMLHandler = new XMLHandler();
xmlR.setContentHandler(myXMLHandler);
xmlR.parse(new InputSource(url.openStream()));
} catch (Exception e) {
System.out.println(e);
}
data = XMLHandler.data;
Now my question is, how can I pass parameters on my php url. Cause on my PHP file I needed some data so that I could validate if it is valid or not.
Let say I need the Username of the person logged on the phone?
On DOM approach I could do something like this
XMLParser parser2 = new XMLParser();
parser2.getXmlFromUrl(URL_FBFRIEND);
//HTTP POST
String url_Getmembermob= URL_FBFRIEND ;
//String xml_getMembermob=null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url_Getmembermob);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("blah", "blah"));
nameValuePairs.add(new BasicNameValuePair("blah", "blah"));
nameValuePairs.add(new BasicNameValuePair("blah", "blah"));
nameValuePairs.add(new BasicNameValuePair("blah", "blah"));
//Log.i("nameValuePairs", "nameValuePairs=" + nameValuePairs);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpclient.execute(httppost);
HttpEntity httpEntity = httpResponse.getEntity();
FBFRIENDS = EntityUtils.toString(httpEntity);
SAX is a push parser. This means that it goes through an XML document, generates events based on it and pushes them forward for you to handle. It does not create an object tree like DOM. But it can help you create a custom object or an object hierarchy of your choice with much less of a memory footprint.
Generated SAX events could be like “uh! oh! I’ve just hit an element openening tag”, “alrighty! I’ve hit an attribute” or “characters! dear god, I’ve located a text value”. An event is usually generated for each XML construct found in the XML document being parsed but not all of them need to be handled. What gets handled is specified by an implementation of a handler (it’s set using
setContentHandler(myXMLHandler)in your example).In order to get specific XML content you need to implement or use an implementation of a specific handler. Your example uses an implementation called
XMLHandler. I don’t know if you yourself implemented it or it’s just some default implementation. From the looks of it it just copies entire content of an XML document into a String member which is not really what you want to do with SAX (DOM will do that better for you).What you need to do now is to create an object with the data you require by implementing a handler. Here’s an example (instead of just printing stuff, you should build objects the way you see fit):
See here for more details.