最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
sax方式XML文件解析之DOM方法
时间:2022-06-25 04:04:16 编辑:袖梨 来源:一聚教程网
代码如下 | 复制代码 |
可以用一下的方式进行解析:
代码如下 | 复制代码 |
public static void parse(File file) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(file); Element root = doc.getDocumentElement(); NodeList childList = root.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { Node child = childList.item(i); if (child instanceof Element) { Element element = (Element) child; NodeList childInfo = element.getChildNodes(); for (int j = 0; j < childInfo.getLength(); j++) { Node node = childInfo.item(j); if (node instanceof Element) { Element e = (Element) node; Log.v("tag", "" + e.getTagName()); Text textNode = (Text) e.getFirstChild(); String text = textNode.getData().trim(); if (e.getTagName().equals("name")) Log.v("tag", "" + text); else if (e.getTagName().equals("sno")) Log.v("tag", "" + text); else if (e.getTagName().equals("sex")) Log.v("tag", "" + text); else if (e.getTagName().equals("phone")) Log.v("tag", "" + text); } } } } } |