国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
使用dom讀取xml數(shù)據(jù)的兩種方式

在讀取XML數(shù)據(jù)的時候可以采用兩種實現(xiàn),可以靈活應(yīng)用。

XML文件為:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
    <book category="COOKING">
      <title lang="en">Everyday Italian</title>
      <author>Giada De Laurentiis</author>
      <year>2005</year>
      <price>30.00</price>
    </book>
    <book category="CHILDREN">
      <title lang="en">Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </book>
    <book category="WEB">
      <title lang="en">XQuery Kick Start</title>
      <author>James McGovern</author>
      <year>2003</year>
      <price>49.99</price>
    </book>
    <book category="EEWEB">
      <title lang="en">Learning XML</title>
      <author>Erik T. Ray</author>
      <year>2003</year>
      <price>39.95</price>
    </book>
</bookstore>
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
   <title lang="en">Everyday Italian</title>
   <author>Giada De Laurentiis</author>
   <year>2005</year>
   <price>30.00</price>
</book>
<book category="CHILDREN">
   <title lang="en">Harry Potter</title>
   <author>J K. Rowling</author>
   <year>2005</year>
   <price>29.99</price>
</book>
<book category="WEB">
   <title lang="en">XQuery Kick Start</title>
   <author>James McGovern</author>
   <year>2003</year>
   <price>49.99</price>
</book>
<book category="EEWEB">
   <title lang="en">Learning XML</title>
   <author>Erik T. Ray</author>
   <year>2003</year>
   <price>39.95</price>
</book>
</bookstore>

實現(xiàn)一:

view plaincopy to clipboardprint?
public static void readXML(String xmlFile){  
          
        try {  
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
            DocumentBuilder db = dbf.newDocumentBuilder();  
            Document doc = db.parse(xmlFile);  
            Element root = doc.getDocumentElement();  
            NodeList ndBooks = root.getElementsByTagName("book");  
            NodeList ndTitles = root.getElementsByTagName("title");  
            NodeList ndAuthors = root.getElementsByTagName("author");  
            NodeList ndYears = root.getElementsByTagName("year");  
            NodeList ndPrices = root.getElementsByTagName("price");  
            if(ndBooks != null){  
                for(int i=0;i<ndBooks.getLength();i++){  
                    Node ndBook = ndBooks.item(i);  
                    Node ndTitle = ndTitles.item(i);  
                    Node ndAuthor = ndAuthors.item(i);  
                    Node ndYear = ndYears.item(i);  
                    Node ndPrice = ndPrices.item(i);  
                    System.out.println(ndBook.getAttributes().getNamedItem("category").getNodeValue());  
                    System.out.println(ndTitle.getAttributes().getNamedItem("lang").getNodeValue());  
                    System.out.println(ndTitle.getFirstChild().getNodeValue());  
                    System.out.println(ndAuthor.getFirstChild().getNodeValue());  
                    System.out.println(ndYear.getFirstChild().getNodeValue());  
                    System.out.println(ndPrice.getFirstChild().getNodeValue());  
                    System.out.println("--------------------------------");  
                }  
            }  
        } catch (ParserConfigurationException e) {  
            e.printStackTrace();  
        } catch (SAXException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }         
    }
public static void readXML(String xmlFile){
  
   try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(xmlFile);
    Element root = doc.getDocumentElement();
    NodeList ndBooks = root.getElementsByTagName("book");
    NodeList ndTitles = root.getElementsByTagName("title");
    NodeList ndAuthors = root.getElementsByTagName("author");
    NodeList ndYears = root.getElementsByTagName("year");
    NodeList ndPrices = root.getElementsByTagName("price");
    if(ndBooks != null){
     for(int i=0;i<ndBooks.getLength();i++){
      Node ndBook = ndBooks.item(i);
      Node ndTitle = ndTitles.item(i);
      Node ndAuthor = ndAuthors.item(i);
      Node ndYear = ndYears.item(i);
      Node ndPrice = ndPrices.item(i);
      System.out.println(ndBook.getAttributes().getNamedItem("category").getNodeValue());
      System.out.println(ndTitle.getAttributes().getNamedItem("lang").getNodeValue());
      System.out.println(ndTitle.getFirstChild().getNodeValue());
      System.out.println(ndAuthor.getFirstChild().getNodeValue());
      System.out.println(ndYear.getFirstChild().getNodeValue());
      System.out.println(ndPrice.getFirstChild().getNodeValue());
      System.out.println("--------------------------------");
     }
    }
   } catch (ParserConfigurationException e) {
    e.printStackTrace();
   } catch (SAXException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }  
}

實現(xiàn)二:

view plaincopy to clipboardprint?
public static void readXML2(String xmlFile){  
        try {  
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
            DocumentBuilder db = dbf.newDocumentBuilder();  
            Document doc = db.parse(xmlFile);  
            Element root = doc.getDocumentElement();//root 指向bookstore節(jié)點  
            NodeList nls = root.getChildNodes();//nls指向bookstore的子節(jié)點book  
            if(nls!= null){  
                for(int i=0;i<nls.getLength();i++){  
                    Node nd = nls.item(i);//輸出每個book節(jié)點的category屬性  
                    if(nd.getNodeType() == Node.ELEMENT_NODE){  
                        System.out.println("book:category:" + nd.getAttributes().getNamedItem("category").getNodeValue());  
                    }  
                    //以下遍歷book節(jié)點的所有子節(jié)點  
                    for(Node nde = nd.getFirstChild();nde!=null; nde = nde.getNextSibling()){  
                        if(nde.getNodeType() == Node.ELEMENT_NODE){  
                            //輸出title節(jié)點值  
                            if(nde.getNodeName().equals("title")){  
                                System.out.println("----title:" + nde.getFirstChild().getNodeValue());  
                            }  
                            //輸出author節(jié)點值  
                            if(nde.getNodeName().equals("author")){  
                                System.out.println("----author:" + nde.getFirstChild().getNodeValue());  
                            }  
                            //輸出year節(jié)點值  
                            if(nde.getNodeName().equals("year")){  
                                System.out.println("----year:" + nde.getFirstChild().getNodeValue());  
                            }  
                            //輸出price節(jié)點值  
                            if(nde.getNodeName().equals("price")){  
                                System.out.println("----price:" + nde.getFirstChild().getNodeValue());  
                            }  
                              
                              
                        }  
                    }  
                      
                }  
            }  
        } catch (ParserConfigurationException e) {  
            e.printStackTrace();  
        } catch (SAXException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }
public static void readXML2(String xmlFile){
   try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(xmlFile);
    Element root = doc.getDocumentElement();//root 指向bookstore節(jié)點
    NodeList nls = root.getChildNodes();//nls指向bookstore的子節(jié)點book
    if(nls!= null){
     for(int i=0;i<nls.getLength();i++){
      Node nd = nls.item(i);//輸出每個book節(jié)點的category屬性
      if(nd.getNodeType() == Node.ELEMENT_NODE){
       System.out.println("book:category:" + nd.getAttributes().getNamedItem("category").getNodeValue());
      }
      //以下遍歷book節(jié)點的所有子節(jié)點
      for(Node nde = nd.getFirstChild();nde!=null; nde = nde.getNextSibling()){
       if(nde.getNodeType() == Node.ELEMENT_NODE){
        //輸出title節(jié)點值
        if(nde.getNodeName().equals("title")){
         System.out.println("----title:" + nde.getFirstChild().getNodeValue());
        }
        //輸出author節(jié)點值
        if(nde.getNodeName().equals("author")){
         System.out.println("----author:" + nde.getFirstChild().getNodeValue());
        }
        //輸出year節(jié)點值
        if(nde.getNodeName().equals("year")){
         System.out.println("----year:" + nde.getFirstChild().getNodeValue());
        }
        //輸出price節(jié)點值
        if(nde.getNodeName().equals("price")){
         System.out.println("----price:" + nde.getFirstChild().getNodeValue());
        }
       
       
       }
      }
     
     }
    }
   } catch (ParserConfigurationException e) {
    e.printStackTrace();
   } catch (SAXException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
}

第二種方法更可取些。注意理解

調(diào)用方法:

view plaincopy to clipboardprint?
public static void main(String[] args) {  
    String xmlFile = "E:\\workspace_g2\\TT\\src、\\books.xml";  
    readXML2(xmlFile);  
          
    }
public static void main(String[] args) {
    String xmlFile = "E:\\workspace_g2\\TT\\src、\\books.xml";
    readXML2(xmlFile);
  
}

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
配置java才解析xml文件
java讀XML文件例子
使用Vitamio打造自己的Android萬能播放器(9)
用DOM/JDOM解析XML文件
Java讀取XML文件代碼實例
Java 使用httpclient Post與cxf 發(fā)布的Webservice通信
更多類似文章 >>
生活服務(wù)
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服