SAX parsing using XML file in android - android

<root>
<subelement>
<item></item>
<valid></valid>
</subelement>
<subelement>
<item></item>
<valid></valid>
</subelement>
<valid></valid>
</root>
In the above response i need to parse and get the subelement value to arraylist and get the root element "valid" tag value String ....How can i parse it.I have did sample but this kind of format is little bit confusing me.

you can use defaulthandler for this work and write code same this for it
boolean subelement,item,valid;rootvalid;
#Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if(localName.equal("subelement"))
subelement=true;
if(localName.equal("item"))
item=true;
if(subelement && localName.equal("valid"))
valid=true;
else if(localName.equal("valid"))
rootvalid=true;
}
public void endElement(String namespaceURI, String localName, String qName) throws SAXException
{
if(localName.equal("subelement"))
subelement=false;
if(localName.equal("item"))
item=false;
if(subelement && localName.equal("valid"))
valid=false;
else if(localName.equal("valid"))
rootvalid=false;
}
public void characters(char ch[], int start, int length)
{
if(item)
//add item to arraylist
if(valid)
//add valid to arraylist
if(validroot)
//save valid of root}

Read this Documantation and follwo this tutorial. It will surely help out.

Related

Parse XML and put the data into spinner

I am new to andriod programming can any help on how to parse a xml file and put the data into a spinner please help.
The XML file link is as follows :http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml
Thanks
since your xml file is large is better that use sax parser .for using from sax parser you should use DeafaultHandler class and override its methods and in its startElement method you should write thing same following for get values:
public void startElement(String arg0, String localName, String arg2,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("marker")) {
String currency = attributes.getValue("currency ");
String rat= attributes.getValue("rat");
//and ...
}
}
}

how get attribute value of xml node while parsing in android (Using BaseFeedParser)

I am developing an application where I want to parse a xml. my node structure ia as follow
<MainNode title="firstNode">
<Subnode>value</Subnode>
</MainNode>
Now I want to read title of first node but I don't get it how to read this. Does any one done this before. Please give some example.
Thank You
Note--Below code tried
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
super.startElement(uri, localName, name, attributes);
if (localName.equalsIgnoreCase(MAIN)){
this.RegisterMessage = new RegisterMessage();
Log.v("AttValue",attributes.getValue("title").toString());
}
}
I tested this code in debugger but control doesn't came to if statement.
Note:-- SAX Parser is looking more complicated for me so I just shifted for BaseFeedParser it not solved my question but it is working as I want & still the question is same how to read nodes attribute in BaseFeedParser.
Inside your handler class, implement parsing for MainNode as:
#Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equals("MainNode"))
{
/** Get attribute value */
String strTitle = attributes.getValue("title");
Log.i("Title",strTitle);
}
}
You can follow my article series on Android XML : http://xjaphx.wordpress.com/2011/10/09/android-xml-adventure-parsing-data-with-saxparser/
Very easy for beginners, full source code provided, line by line instructions :)

how to parse xml from EditText?

i try to build an application with xml parser.
ex : http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser
but i want to parse xml file from EditText, how it work ?
Change this line:
xr.parse(new InputSource(sourceUrl.openStream()));
to
String xmlString = editText.getText().toString();
StringReader reader = new StringReader(xmlString);
xr.parse(new InputSource(reader));
You have several ways to parse XML, the most widely used being SAX and DOM. The choice is quite strategical as explained in this paper.
Here is a short explanation of SAX:
You will need some imports:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import android.util.Xml;
Create your own XML SAX DefaultHandler:
class MySaxHandler extends DefaultHandler {
// Override the methods of DefaultHandler that you need.
// See [this link][3] to see these methods.
// These methods are going to be called while the XML is read
// and will allow you doing what you need with the content.
// Two examples below:
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// This is called each time an openeing XML tag is read.
// As the tagname is passed by parameter you know which tag it is.
// You also have the attributes of the tag.
// Example <mytag myelem="12"> will lead to a call of
// this method with the parameters:
// - localName = "mytag"
// - attributes = { "myelem", "12" }
}
public void characters(char[] ch, int start, int length) throws SAXException {
// This is called each time some text is read.
// As an example, if you have <myTag>blabla</myTag>
// this will be called with the parameter "blabla"
// Warning 1: this is also called when linebreaks are read and the linebreaks are passed
// Warning 2: you have to memorize the last open tag to determine what tag this text belongs to (I usually use a stack).
}
}
Extract the XML as a String from the EditText. Let's call it xmlContent
Create and initialize your XML parser:
final InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(xmlContent.getBytes()));
final MySaxHandler handler = new MySaxHandler();
And then, make the XML content to be read by the parser. This will lead your MySaxHandler to have its various method called while the reading is in progress.
Xml.parse(reader, handler);

Android SAX Parser not reading net work loaded document (Character de/encoding issue)?

I'm using Android to read a document off the net, surprise I'm writing here because I have an issue. For lots of sites I have no issues, but for some sites the xml parser in android is "grumpy". I suspect it's something to do with the Character encoding, but I'm not sure exactly what. In particular if I download the file with "wget" and feed it to android, it works fine....
Android's error message,
03-23 21:54:47.383: ERROR/xml(9062): org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 62: syntax error
The xml when I download it seems fine.
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
...
My sample android application....
package com.example.android.helloactivity;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class HelloActivity extends Activity {
class EnclosureHandler extends DefaultHandler {
#Override
public void characters(char[] ch, int start, int length)
throws SAXException {
}
#Override
public void endElement(String uri, String localName, String name)
throws SAXException {
}
#Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
Log.i("xml", "lname is : " + qName);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_activity);
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
InputSource is = new InputSource(new URL(
"http://www.hbo.com/podcasts/billmaher/podcast.xml")
.openStream());
sp.parse(is, new EnclosureHandler());
} catch (Throwable t) {
Log.e("xml", t.toString());
Toast.makeText(getApplicationContext(), t.toString(),
Toast.LENGTH_LONG).show();
}
}
}
Turns out that character encoding is not the issue. The HBO.com web site returns different content based on the USER-AGENT: header. So if you use Android to talk with the hbo.com site, they return a message about how you could use there own android client to access the site. They probably are trying to help people using web browsers. Changing the USER-AGENT then caused the above program to get the correct (and parse-able) xml document.

unable to get full video URL from sax xml parsing in android app

As I am new to Android development,
I have used SAX xml parsing in my Android app.
It is working fine but in URL when it got "&" symbol it simply discard the next all url part.
I think not sure is this problem of charcaters method of that DefaultHandler class?
public void characters(char[] ch, int start, int length)throws SAXException {
if (currentElement)
{
currentValue = new String(ch, start, length);
currentElement = false;
}
}
thanks for the help.
Replace "&" characters in you XML with escape sequences. XML file shouldn't contain "&" characters out of CDATA fields.

Categories

Resources