最简单的例子了
待解析的zzk.xml文件,只有一个参数utc。
<?xml version="1.0" encoding="UTF-8"?>
<zzk>
<config utc = "1625043055"></config>
</zzk>
解析函数
public static String queryXML(String path)
{
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
Document doc = dbBuilder.parse(new FileInputStream(path));
NodeList nList = doc.getElementsByTagName("config");
Element ielement = (Element) nList.item(0);
return ielement.getAttribute("utc");
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
使用
String path = getStoragePath(mContext);
String strFilePath = path + "/zzk.xml";
utc_xml =queryXML(strFilePath); //得到xml里的utc
|