Dom解析xmlDemo

package analysis_xml.dom;
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 Analysis_XmlByDom 
{
	public static void main(String[] args)
    {
	    long lasting =System.currentTimeMillis();
	    try{
			    File f=new File("src/DefaultXml.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("\t车主地址:" +  doc.getElementsByTagName("addr").item(i).getFirstChild().getNodeValue());
			    }
	    }
	    catch(Exception e)
	    {
	    	e.printStackTrace();
	    }
	}

}

你可能感兴趣的:(dom解析xml)