解析xml

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;
}


新手写的文章都是这么浅显,我的路还很长很长……
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值