隨筆-0  評論-15  文章-10  trackbacks-0

          package cn.zbvc.java;
          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;

          /*
           *java訪問xml文件
           *
          */

          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("\r\n找到一篇賬號. 所屬區域: "   + 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);}
              
          }
          }

          /**
           *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>
           
           
           
           
           
           
          */
          posted on 2011-07-10 10:03 呂鵬-Roc 閱讀(793) 評論(0)  編輯  收藏 所屬分類: Java常用代碼
          主站蜘蛛池模板: 钦州市| 遂川县| 济源市| 鸡泽县| 涞源县| 南开区| 丹凤县| 安多县| 南昌县| 紫金县| 黄平县| 宝坻区| 东台市| 京山县| 墨脱县| 仙游县| 揭西县| 天水市| 阿克| 曲阜市| 拉萨市| 筠连县| 民丰县| 平利县| 喜德县| 色达县| 云安县| 乌审旗| 尚志市| 漠河县| 攀枝花市| 肥城市| 根河市| 壤塘县| 黎城县| 子长县| 清水河县| 靖西县| 偏关县| 吴川市| 普陀区|