FileInfo是我自己定义的类
使用时候直接调用这个方法就可以:
新手写的文章都是这么浅显,我的路还很长很长……
public class XMLParser {
public static List<FileInfo> parserConfig(InputStream is) {
List<FileInfo> list = new ArrayList<FileInfo>();
FileInfo temp = null;
DocumentBuilderFactory factory = null;
DocumentBuilder db = null;
try {
factory = DocumentBuilderFactory.newInstance();
db = factory.newDocumentBuilder();
Document document = db.parse(is);
Element rootElement = document.getDocumentElement();
NodeList nodeList = rootElement.getElementsByTagName("item");
Element itemElement = null;
NodeList itemChildList = null;
for (int i = 0; i < nodeList.getLength(); i++) {
temp = new FileInfo();
itemElement = (Element) nodeList.item(i); // item
itemChildList = itemElement.getChildNodes();
for (int j = 0; j < itemChildList.getLength(); j++) {
System.out.println(itemChildList.item(j).getNodeName());
if ("name".equals(itemChildList.item(j).getNodeName())) {
System.out.println("valeu:"
+ itemChildList.item(j).getFirstChild()
.getNodeValue());
temp.folderName = new String(itemChildList.item(j)
.getFirstChild().getNodeValue().trim()
.getBytes(), "utf-8");
}
if ("folder".equals(itemChildList.item(j).getNodeName())) {
System.out.println("valeu:"
+ itemChildList.item(j).getFirstChild()
.getNodeValue());
temp.folderPath = new String(itemChildList.item(j)
.getFirstChild().getNodeValue().trim()
.getBytes(), "utf-8");
}
}
list.add(temp);
}
} catch (Exception e) {
Log.e("downloadmanager", "parserConfig:" + e.toString());
} finally {
factory = null;
db = null;
}
return list;
}
}
使用时候直接调用这个方法就可以:
public static List<FileInfo> paseXml(Context ctx) {
List<FileInfo> listFolder = null;
try {
listFolder = XMLParser.parserConfig(ctx.getResources().getAssets()
.open("config.xml"));
} catch (IOException e) {
e.printStackTrace();
}
return listFolder;
}
新手写的文章都是这么浅显,我的路还很长很长……