最新下载
热门教程
- 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); } } } } } |
相关文章
- 原神振晶的应用研究第二天怎么过 04-29
- 新不良人六气宗阵容教学攻略 04-29
- 保卫萝卜4周赛4.29怎么过 04-29
- 逆水寒手游此心怀秋人间任务怎么做 04-29
- 蛋仔派对猫鼠奶酪假期怎么玩 04-29
- 逆水寒手游毫厘之义成就怎么达成 04-29