使用 Android 在 xml 中搜索特定属性值和文本值
Searching for specific attribute value and text value in xml with Android
我需要以某种方式在特定节点中找到特定值,然后将其与属性值一起显示在文本视图中。
http://www.bsi.si/_data/tecajnice/dtecbs-l.xml 这是我需要搜索的 xml 文件,它包含多种货币的汇率列表。部分问题还在于具有相同名称的节点。
我正在从网络上成功访问它。我只是找不到如何找到特定属性值然后使用它来获取所需文本的示例或教程。
我在 Android 工作室工作。第一个用户将 select 汇率日期,然后他 select 想要显示的货币,因此它将显示 [Selected currency] = [Value]
所以我认为,如果我至少可以将搜索范围缩小到特定日期,那将是一件好事。
我之前使用 Xpath 成功地做到了这一点,但是这个文件太大了,所以我被迫使用 SAX,但如果有其他示例,我会开放。
我正在使用此示例作为模板:http://www.pcsalt.com/android/xml-parsing-using-saxparser-android/
我不知道我是否应该使用这种结构,但这是我发现与我的案例最相关的一种。
我对 java 有一些基本了解,但 none 对 sax 或任何其他解析器有一些了解。
编辑:
我现在有工作代码 returns 所有货币名称和汇率从 selected 日期开始,在这个例子中是 2007-01-01。现在我想使用我在 FirstPage activity 中的日期选择器的用户输入来查找 selected 日期的汇率和名称,但我无法让它工作。我不知道这是否与我用 3 个整数(日、月、年)生成字符串值的方式有关,或者是否与此字符串从 FirstPage activity 到此 XMLHelepr class 的转换有关.或者 sax 可能不会使用这个变量。
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);
}
}
我在 FirstPage activity 上使用日期选择器并将值输入字符串以使用它来与来自 XMLHelper.Im 的属性 "datum" 进行比较,使用 geter-setter 设置 izbraniDatum 值在此 activity 中,它在第一个 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());
}
};
和 XMLHelper 中的比较行:
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(datum.equals(firstPage.getIzbraniDatum())){
有人帮忙吗?
我让它工作了。我需要做的就是使保存来自 FirstPage 的日期选择器的日期的字符串变量成为静态的,因此它现在在 activity 开始时声明为 public static izDatum ="";然后从 datepicker 获取字符串值,然后在编译它时在 XMLHelper class 上简单地计算它 if(date.equals(firstPage.izDatum)
我需要以某种方式在特定节点中找到特定值,然后将其与属性值一起显示在文本视图中。 http://www.bsi.si/_data/tecajnice/dtecbs-l.xml 这是我需要搜索的 xml 文件,它包含多种货币的汇率列表。部分问题还在于具有相同名称的节点。
我正在从网络上成功访问它。我只是找不到如何找到特定属性值然后使用它来获取所需文本的示例或教程。
我在 Android 工作室工作。第一个用户将 select 汇率日期,然后他 select 想要显示的货币,因此它将显示 [Selected currency] = [Value]
所以我认为,如果我至少可以将搜索范围缩小到特定日期,那将是一件好事。
我之前使用 Xpath 成功地做到了这一点,但是这个文件太大了,所以我被迫使用 SAX,但如果有其他示例,我会开放。
我正在使用此示例作为模板:http://www.pcsalt.com/android/xml-parsing-using-saxparser-android/ 我不知道我是否应该使用这种结构,但这是我发现与我的案例最相关的一种。
我对 java 有一些基本了解,但 none 对 sax 或任何其他解析器有一些了解。
编辑:
我现在有工作代码 returns 所有货币名称和汇率从 selected 日期开始,在这个例子中是 2007-01-01。现在我想使用我在 FirstPage activity 中的日期选择器的用户输入来查找 selected 日期的汇率和名称,但我无法让它工作。我不知道这是否与我用 3 个整数(日、月、年)生成字符串值的方式有关,或者是否与此字符串从 FirstPage activity 到此 XMLHelepr class 的转换有关.或者 sax 可能不会使用这个变量。
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);
}
}
我在 FirstPage activity 上使用日期选择器并将值输入字符串以使用它来与来自 XMLHelper.Im 的属性 "datum" 进行比较,使用 geter-setter 设置 izbraniDatum 值在此 activity 中,它在第一个 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());
}
};
和 XMLHelper 中的比较行:
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(datum.equals(firstPage.getIzbraniDatum())){
有人帮忙吗?
我让它工作了。我需要做的就是使保存来自 FirstPage 的日期选择器的日期的字符串变量成为静态的,因此它现在在 activity 开始时声明为 public static izDatum ="";然后从 datepicker 获取字符串值,然后在编译它时在 XMLHelper class 上简单地计算它 if(date.equals(firstPage.izDatum)