xml解析 DOM(JAXP Crimson解析器)

<?xml version="1.0" encoding="GB2312"?>
<result>
<value>
<no>123</no>
<addr>beijing</addr>
</value>
<value>
<no>456</no>
<addr>shanghai</addr>
</value>
</result>
package com.xml;
import java.io.*;  
import org.w3c.dom.*;  
import org.xml.sax.SAXException;

import javax.xml.parsers.*;  

public class XMLParseTest 
{
	public static void main(String args[])
	{
		File xmlFile = new File("c:/test.xml");
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(xmlFile);
			NodeList nodeList = doc.getElementsByTagName("value");
			for(int i=0;i<nodeList.getLength();i++)
			{
				System.out.println("车牌号码:"+doc.getElementsByTagName("no").item(i).getFirstChild().getNodeValue());
				System.out.println("车主地址:"+doc.getElementsByTagName("addr").item(i).getFirstChild().getNodeValue());
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

posted @ 2012-03-09 05:27  梦见舟  阅读(144)  评论(0编辑  收藏  举报