dom

package com.huawe.zlk;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Dom
{
    public static void main(String[] args)
    {      
        try
        {
            File f=new File("NewFile.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.println("姓名:"+doc.getElementsByTagName("name").item(i).getFirstChild().getNodeValue());
                System.out.println("地址:"+doc.getElementsByTagName("address").item(i).getFirstChild().getNodeValue());
            }
           
        }
        catch (DOMException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        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();
        }
    }
}

你可能感兴趣的:(dom)