Parse XML and put the data into spinner - android

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 ...
}
}
}

Related

How to replace special character from HTML document

I have a string "© 2015" in my HTML document. I'm parsing the HTML document using TagHandler.
opinion_description.setText(Html.fromHtml(description, this, new Html.TagHandler() {
#Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
}
}));
I'm not handling anything inside handleTag(), as the tagHandler itself is handling everything. But it's not parsing the above mentioned string. So, I'm manually replacing the string with "".
if(description.contains("Ã-Â"))
description.replace("Ã-Â","");
But it's not working. Is there any better way to parse this string.
Thanks in advance.

SAX parsing using XML file in 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.

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);

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