创建一个定义对象是什么的类:
public class Term implements Serializable {
private static final long serialVersionUID = 1L;
private String word;
private String abbr1;
private String abbr2;
private String definition;
private String formula;
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getAbbr1() {
return abbr1;
}
public void setAbbr1(String abbr1) {
this.abbr1 = abbr1;
}
public String getAbbr2() {
return abbr2;
}
public void setAbbr2(String abbr2) {
this.abbr2 = abbr2;
}
public String getDefinition() {
return definition;
}
public void setDefinition(String definition) {
this.definition = definition;
}
public String getFormula() {
return formula;
}
public void setFormula(String formula) {
this.formula = formula;
}
}
然后使用 XML 解析器解析您的 XML:
public ArrayList<Term> getTerms(){
ArrayList<Term> termsList = new ArrayList<Term>();
//create array lists for the four attributes of each term
ArrayList<String> word = new ArrayList<String>();
ArrayList<String> abbr1 = new ArrayList<String>();
ArrayList<String> abbr2 = new ArrayList<String>();
ArrayList<String> definition = new ArrayList<String>();
ArrayList<String> formula = new ArrayList<String>();
//Strings for each of the attributes
String currentWord = null;
String currentAbbr1 = null;
String currentAbbr2 = null;
String currentDefinition = null;
String currentFormula = null;
//try to parse the XML into the four array lists
try{
//get the xml document and put it into a reader
XmlPullParser parser = getResources().getXml(R.xml.terminology);
//set the eventType and the boolean to test if finished.
int eventType = parser.getEventType();
boolean done = false;
/*
* while statement that will read through the xml document and includes
* a switch case block that will go through the tags and put the attributes
* into the appropriate array lists
*/
while (eventType !=XmlPullParser.END_DOCUMENT && !done){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("terminology")){
}else if (word != null && abbr1 != null && abbr2 != null && definition != null && formula != null){
if (name.equalsIgnoreCase("term")){
}else if (name.equalsIgnoreCase("word")){
currentWord = new String(parser.getAttributeValue(0));
}else if (name.equalsIgnoreCase("abbr1")){
currentAbbr1 = new String(parser.getAttributeValue(0));
}else if (name.equalsIgnoreCase("abbr2")){
currentAbbr2 = new String(parser.getAttributeValue(0));
}else if (name.equalsIgnoreCase("definition")){
currentDefinition = new String(parser.getAttributeValue(0));
}else if (name.equalsIgnoreCase("formula")){
currentFormula = new String(parser.getAttributeValue(0));
}
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("formula") && currentFormula != null){
formula.add(currentFormula);
}else if (name.equalsIgnoreCase("definition") && currentDefinition != null){
definition.add(currentDefinition);
}else if (name.equalsIgnoreCase("abbr2") && currentAbbr2 != null){
abbr2.add(currentAbbr2);
}else if (name.equalsIgnoreCase("abbr1") && currentAbbr1 != null){
abbr1.add(currentAbbr1);
}else if (name.equalsIgnoreCase("word") && currentWord != null){
word.add(currentWord);
}else if (name.equalsIgnoreCase("term")){
}else if (name.equalsIgnoreCase("terminology")){
done = true;
}
break;
}
eventType = parser.next();
}
}
catch (FileNotFoundException e){
}
catch (IOException e){
}
catch (Exception e){
}
for (int i=0; i < word.size(); i++){
Term term = new Term();
term.setWord(word.get(i));
term.setAbbr1(abbr1.get(i));
term.setAbbr2(abbr2.get(i));
term.setDefinition(definition.get(i));
term.setFormula(formula.get(i));
termsList.add(term);
}
return termsList;
}
这对我来说太棒了。这是很多代码,但它实际上也很快。我注意到没有滞后时间,我的 XML 中有近 30 个“术语”,每个都有 5 个属性。只需将您的 XML 存储在 res/xml 下(您可能必须创建 XML 文件夹)。