I need to somehow find specific value in specific node and then display it in text view along with attribute value. http://www.bsi.si/_data/tecajnice/dtecbs-l.xml this is the xml file i need to search, its list of exchange rates for multiple currencies. Part of the problem is also nodes that have the same name.
I'm successfully accessing it from the web. I just could not find example or tutorial how to find specific attribute value and then using it to get desired text.
Im working in Android Studio. Firs user will select the date of the exchange rate, then he selects desired currencies he wants to display, so it will display [Selected currency] = [Value]
So I think that if I can at least narrow the search to specific date it would be something.
I did this successfully before with Xpath but this file is too big, so im forced to use SAX, but im open to other examples if there is one.
I'm using this example as a template:http://www.pcsalt.com/android/xml-parsing-using-saxparser-android/ I don't know if I should use this structure, but it's the one I found most relevant to my case.
I have some basic knowledge with java, but none with sax or any other parser.
EDIT:
I now have working code that returns all currencies names and exchange rates from selected date, in this example below 2007-01-01. Now I want to use user input from date picker that i have in FirstPage activity to look for exchange rates and names from that selected date, but i just can't get it to work. I don't know if it's something with the way i make string value out of 3 integers (day, month, year) or is it something with transition of this string from FirstPage activity to this XMLHelepr class. Or maybe sax won't take this variable.
public class XMLHelper extends DefaultHandler {
/**
* The URL to be parsed
*/
private String URL_MAIN = "http://www.bsi.si/_data/tecajnice/dtecbs-l.xml";
String TAG = "XMLHelper";
String datum ;
String oznaka;
Boolean currTag = false;
String currTagVal = "";
private PostValue post = null;
private ArrayList<PostValue> posts = new ArrayList<PostValue>();
public ArrayList<PostValue> getPostsList() {
return this.posts;
}
public FirstPage firstPage;
public void get() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser mSaxParser = factory.newSAXParser();
XMLReader mXmlReader = mSaxParser.getXMLReader();
mXmlReader.setContentHandler(this);
InputStream mInputStream = new URL(URL_MAIN).openStream();
mXmlReader.parse(new InputSource(mInputStream));
} catch (Exception e) {
// Exceptions can be handled for different types
// But, this is about XML Parsing not about Exception Handling
Log.e(TAG, "Exception: " + e.getMessage());
}
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.i(TAG, "TAG: " + localName);
currTag = true;
currTagVal = "";
int len = attributes.getLength();
// Loop through all attributes and save them as needed
for(int i = 0; i < len; i++)
{
String sAttrName = attributes.getLocalName(i);
if(sAttrName.compareTo("datum") == 0)
{
datum = attributes.getValue(i);
Log.i(TAG, "Datum: " + datum);
}
}
int len2 = attributes.getLength();
// Loop through all attributes and save them as needed
for(int i = 0; i < len2; i++){
String sAttrName = attributes.getLocalName(i);
if(sAttrName.compareTo("oznaka") == 0)
{
oznaka = attributes.getValue(i);
Log.i(TAG, "Oznaka: " + oznaka);
}
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(datum.equals("2007-01-01")){
if(oznaka.equals(oznaka)) {
post = new PostValue();
if (currTag) {
currTagVal = currTagVal + new String(ch, start, length);
currTag = false;
post.setOznaka(oznaka);
post.setVrednost(currTagVal);
}
}
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currTag = false;
if(currTagVal!="")
posts.add(post);
}
}
im using date picker on FirstPage activity and geting values into String to use it to compere with attribute "datum" from XMLHelper.Im seting izbraniDatum value with geter-setter in this activity, and it displays correct on the textview in first activity
dp1 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
leto1= year;
mesec1= monthOfYear +1;
dan1 = dayOfMonth;
izbraniDatum= checkDigit(year)+"-"+checkDigit(monthOfYear + 1)+"-"+checkDigit(dayOfMonth);
setIzbraniDatum(izbraniDatum);
prikaz1.setText(getIzbraniDatum());
}
};
and the comparing line in XMLHelper:
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(datum.equals(firstPage.getIzbraniDatum())){
anyone help?
I got it to work. All I needed to do is to make string variable that holds date from FirstPage's date picker static, so it's now decrared at start of activity as public static izDatum =""; and then gets string value from datepicker and then its is simply caled on XMLHelper class when compering it if(date.equals(firstPage.izDatum)