关于java读取xml文件

1.创建一个名字叫conf的source folder,并在其下面创建一个info.xml有如下类容: A1234 河南省郑州市 B1234 河南省郑州市二七区 ====================== 2.Java代码: import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class MyXMLReader2DOM { public static void main(String arge[]) { long lasting = System.currentTimeMillis(); try { File f = new File("conf/info.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(f); NodeList nl = doc.getElementsByTagName("VALUE"); for (int i = 0; i < nl.getLength(); i++) { System.out.print("车牌号码:"+ doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue()); System.out.println("车主地址:"+ doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue()); System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + "毫秒"); } } catch (Exception e) { e.printStackTrace(); } } } ======================= 运行结果: 车牌号码:A1234车主地址:河南省郑州市 运行时间:230毫秒 车牌号码:B1234车主地址:河南省郑州市二七区 运行时间:230毫秒
posted @ 2013-11-06 21:01  黄昏的玫瑰  阅读(122)  评论(0)    收藏  举报