2014-12-03 回答
读取数据代码:getvalue(this,"第四");
public string getvalue(context context, string strtext) {
string strdata = "";
documentbuilderfactory factory = null;
documentbuilder builder = null;
document document = null;
inputstream inputstream = null;
// 首先找到xml文件
factory = documentbuilderfactory.newinstance();
try {
// 找到xml,并加载文档
builder = factory.newdocumentbuilder();
inputstream = context.getresources().getassets()
.open("data.xml");
document = builder.parse(inputstream);
// 找到根element
element root = document.getdocumentelement();
nodelist nodes = root.getelementsbytagname("item");
// 遍历根节点所有子节点
element cardelement;
string strname;
string strvalues;
for (int i = 0; i < nodes.getlength(); i++) {
cardelement = (element) (nodes.item(i));
strname = cardelement.getattribute("value");
element evalues = (element) cardelement
.getelementsbytagname("item").item(0);
strvalues = evalues.getfirstchild().getnodevalue();
if (strvalues.equals(strtext)) {
strdata = strname;
break;
}
if (i == nodes.getlength() - 1) {
log.v(tag, "未查到相关信息。。。");
strdata = "";
}
}
} catch (ioexception e) {
e.printstacktrace();
} catch (saxexception e) {
e.printstacktrace();
} catch (parserconfigurationexception e) {
e.printstacktrace();
} finally {
try {
inputstream.close();
} catch (ioexception e) {
e.printstacktrace();
}
}
return strdata;
}