writing a parser to read xml data.
Below is a simple script for reading data from any xml file. all you need to know is send a xml file and reading output data from a hashmap.
ParserActivity.java
package com.example.parsers;
import java.net.URL;
import java.util.HashMap;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ParserActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url_value = " ";
try {
URL url = new URL(url_value);
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
HandlerData handler = new HandlerData();
sp.parse(new InputSource(url.openStream()), handler);
getData outdata = handler.getParseddata();
HashMap<String, String> finalhash = new HashMap<String, String>();
finalhash = outdata.gethashdata();
TextView txt = (TextView) findViewById(R.id.text);
txt.setText("abcd");
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
}
HandlerData.java
package com.example.parsers;
import java.util.ArrayList;
import java.util.HashMap;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
class HandlerData extends DefaultHandler {
getData data = new getData();
public getData getParseddata() {
return this.data;
}
HashMap<String, String> outcontent = new HashMap<String, String>();
HashMap<String, Boolean> ischeck = new HashMap<String, Boolean>();
String temp = null;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
String middle_content = new String(ch, start, length);
if (ischeck.get(temp)) {
outcontent.put(temp, middle_content);
ischeck.put(temp, false);
}
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
data.sethashdata(outcontent);
ArrayList<String> keys= new ArrayList<String>(outcontent.keySet());
for(String key : keys)
{
Log.w("outputdata",key+"-"+outcontent.get(key));
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
int total_attributes = attributes.getLength();
if (total_attributes > 0) {
while (total_attributes > 0) {
total_attributes--;
String att = "" + attributes.getQName(total_attributes);
String value = attributes.getValue(att);
outcontent.put(att, value);
}
}
ischeck.put(qName, true);
temp = qName;
}
}
Below is a simple script for reading data from any xml file. all you need to know is send a xml file and reading output data from a hashmap.
ParserActivity.java
package com.example.parsers;
import java.net.URL;
import java.util.HashMap;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ParserActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url_value = " ";
try {
URL url = new URL(url_value);
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
HandlerData handler = new HandlerData();
sp.parse(new InputSource(url.openStream()), handler);
getData outdata = handler.getParseddata();
HashMap<String, String> finalhash = new HashMap<String, String>();
finalhash = outdata.gethashdata();
TextView txt = (TextView) findViewById(R.id.text);
txt.setText("abcd");
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
}
HandlerData.java
package com.example.parsers;
import java.util.ArrayList;
import java.util.HashMap;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Log;
class HandlerData extends DefaultHandler {
getData data = new getData();
public getData getParseddata() {
return this.data;
}
HashMap<String, String> outcontent = new HashMap<String, String>();
HashMap<String, Boolean> ischeck = new HashMap<String, Boolean>();
String temp = null;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
String middle_content = new String(ch, start, length);
if (ischeck.get(temp)) {
outcontent.put(temp, middle_content);
ischeck.put(temp, false);
}
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
data.sethashdata(outcontent);
ArrayList<String> keys= new ArrayList<String>(outcontent.keySet());
for(String key : keys)
{
Log.w("outputdata",key+"-"+outcontent.get(key));
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
int total_attributes = attributes.getLength();
if (total_attributes > 0) {
while (total_attributes > 0) {
total_attributes--;
String att = "" + attributes.getQName(total_attributes);
String value = attributes.getValue(att);
outcontent.put(att, value);
}
}
ischeck.put(qName, true);
temp = qName;
}
}
getData.java
package com.example.parsers;
import java.util.HashMap;
public class getData {
HashMap<String, String> content = new HashMap<String, String>();
public void sethashdata(HashMap<String, String> input) {
this.content = input;
}
public HashMap<String, String> gethashdata() {
return content;
}
}
No comments:
Post a Comment