通过内存, 生成DOM树结构, 简单,快 适合于小文件
package com.Myproject.test;
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ZipFilenameFilter {
// DOM 生成和解析XML文档
public static void main(String[] args) {
File ff=new File("e:\\22.txt");
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(ff);
NodeList weathers=doc.getElementsByTagName("Weather");
for (int i = 0; i < weathers.getLength(); i++) {
Node weather=weathers.item(i);
for (Node node=weather.getFirstChild(); node!=null; node=node.getNextSibling()) {
if (node.getNodeType()==Node.ELEMENT_NODE) {
System.out.println(node.getNodeName());
if (node.getFirstChild()!=null) {
System.out.println(node.getFirstChild().getNodeValue());
}
}
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}