java讀取xml檔案簡單一實例

來源:互聯網
上載者:User

XML具體檔案如下:

 代碼如下 複製代碼

<?xml version="1.0" encoding="gbk"?>      
<Accounts>
<Account type="by0003"> 
<code>100001</code>
<pass>123</pass>
<name>李四</name> 
<money>1000000.00</money> 
</Account> 
<Account type="hz0001"> 
<code>100002</code>
<pass>123</pass>
<name>張三</name> 
<money>1000.00</money> 
</Account> 
</Accounts>

java xml讀取類如下

 代碼如下 複製代碼

import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class xmljava

public static void main(String args[])
    {    
          Element element=null;
          File f =new File("a.xml");
          DocumentBuilder db=null;//documentBuilder為抽象不能直接執行個體化(將XML檔案轉換為DOM檔案)
          DocumentBuilderFactory dbf=null; 
      try{
          
          dbf= DocumentBuilderFactory.newInstance(); //返回documentBuilderFactory對象   
          db =dbf.newDocumentBuilder();//返回db對象用documentBuilderFatory獲得返回documentBuildr對象
          Document dt= db.parse(f); //得到一個DOM並返回給document對象
          element = dt.getDocumentElement();//得到一個elment根項目
           
          System.out.println("根項目:"+element.getNodeName()); //獲得根節點
        NodeList childNodes =element.getChildNodes() ;    // 獲得根項目下的子節點
     
      for (int i = 0; i < childNodes.getLength(); i++)     // 遍曆這些子節點
   {       
       Node node1 = childNodes.item(i); // childNodes.item(i); 獲得每個對應位置i的結點
     if ("Account".equals(node1.getNodeName())) 
      { 
      // 如果節點的名稱為"Account",則輸出Account元素屬性type
      System.out.println("rn找到一篇帳號. 所屬地區: "
      + node1.getAttributes().getNamedItem("type").getNodeValue() + ". ");
      NodeList nodeDetail = node1.getChildNodes();   // 獲得<Accounts>下的節點
      for (int j = 0; j < nodeDetail.getLength(); j++)
       {   // 遍曆<Accounts>下的節點
          Node detail = nodeDetail.item(j);    // 獲得<Accounts>元素每一個節點
            if ("code".equals(detail.getNodeName()))   // 輸出code
            System.out.println("卡號: " + detail.getTextContent());
             else if ("pass".equals(detail.getNodeName())) // 輸出pass
                System.out.println("密碼: " + detail.getTextContent());
             else if ("name".equals(detail.getNodeName())) // 輸出name
                System.out.println("姓名: " + detail.getTextContent());
             else if ("money".equals(detail.getNodeName())) // 輸出money
                 System.out.println("餘額: "+ detail.getTextContent());
       
        }
      }
    }
}
catch(Exception e){System.out.println(e);}
     
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.