So Im pretty new to Java coming from C#! They are pretty similar programming languages, so im getting a hang of it quickly, but there is a problem I have been battling with for a while, that I hope you can help me solve!
So Im using my SAX parser to parse the XML document, and it works fine, but Im having problems parsing the whole xml document, and don't know how to parse the attribute value in the top element.
My xml document is as follows:
This is the code snippet where I believe the problem lies! This code works for parsing all of tecaj elements and their attributes/content values, but not "datum" attribute:
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
this.elementStack.push(qName);
CurrencyModel model = new CurrencyModel();
if("tecajnica".equals(qName))
{
if(attributes != null)
{
model.setDatum(attributes.getValue(0));
}
}
else if("tecaj".equals(qName))
{
if(attributes != null)
{
model.setOznaka(attributes.getValue(0));
model.setSifra(attributes.getValue(1));
}
}
this.objectStack.push(model);
}
So I have a model class that looks like this:
public class CurrencyModel
{
public String getDatum() {
return datum;
}
public void setDatum(String datum) {
this.datum = datum;
}
public String getOznaka() {
return oznaka;
}
public void setOznaka(String oznaka) {
this.oznaka = oznaka;
}
public String getSifra() {
return sifra;
}
public void setSifra(String sifra) {
this.sifra = sifra;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
String datum;
String oznaka;
String sifra;
double value;
@Override
public String toString() {
return "CurrencyModel{" +
"datum=" + datum +
", oznaka='" + oznaka + '\'' +
", sifra='" + sifra + '\'' +
", value=" + value +
'}';
}
}
So each object of type CurrencyModel has its date property that is supposed to get the value of the attribute from its respected "tecajnica" element. It works for all of the other properties but "Datum". At first I was parsing it as Date type, but as that didn't work I tried parsing it as a String. Now it works without any errors, but always sets the object "Datum" property to null...
Any help and suggestions will be much appreciated!!! Thank you in advance!