1. 程式人生 > >解析xml檔案的標籤內容

解析xml檔案的標籤內容

package com.huawei.gts.impl.Dao.document;

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

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class JiexiXml
{
    public static void main(String[] args)
    {

        try
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse("\\src\\com\\huawei\\gts\\impl\\Dao\\document\\document.xml"); // 讀取xml檔案  
            NodeList employees = document.getChildNodes();
            for (int i = 0; i < employees.getLength(); i++)
            {
                Node employee = employees.item(i);
                NodeList employeeInfo = employee.getChildNodes();
                for (int j = 0; j < employeeInfo.getLength(); j++)
                {
                    Node node = employeeInfo.item(j);
                    NodeList employeeMeta = node.getChildNodes();
                    for (int k = 0; k < employeeMeta.getLength(); k++)
                    {
                     System.out.println(employeeMeta.item(k).getNodeName()+ ":" + employeeMeta.item(k).getTextContent());
                    
                    }
                }
            }
            
            System.out.println("解析完畢");
            System.out.println(document.getNodeType());
        }
        catch (Exception e)
        {

        }
    }
}