<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

      1 
      2 package com.parsexml ;
      3 /*測試用的test.xml 請另存
      4  <?xml version="1.0" encoding="gb2312"?>
      5 <books>
      6     <name>test</name>
      7     <book>
      8         <name>rjzjh</name>
      9         <price>jjjjjj</price>
     10     </book>
     11     <test>
     12         <name>hcm</name>
     13         <feng>sadf</feng>
     14     </test>
     15 </books>
     16  */
     17 import javax.swing.*;
     18 import java.io.* ;
     19 import java.net.*;
     20 import javax.xml.parsers.* ;
     21 import org.w3c.dom.* ;
     22 import org.xml.sax.SAXException ;
     23 
     24 public class NewParsexml
     25 {
     26     public static String infotext ;
     27     public NewParsexml ()
     28     {
     29     }
     30     public URL geturl(String str)
     31     {
     32         return (getClass().getResource (str));
     33     }
     34     public  static String parse (String text)
     35     {
     36         // 得到DOM解析器的工廠實例
     37         DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance () ;
     38         try
     39         {
     40             DocumentBuilder dombuilder = domfac.newDocumentBuilder () ;
     41            // InputStream is = new FileInputStream ("src/com/parsexml/test.xml");
     42             InputStream is = NewParsexml.class.getResourceAsStream("test.xml"); 
     43          //   InputStream is = new FileInputStream ("com/parsexml/test.xml");
     44             Document doc = dombuilder.parse (is) ;
     45             Element root = doc.getDocumentElement () ;
     46             NodeList books = root.getChildNodes () ;
     47             if (books != null)
     48             {
     49                 for (int i = 0 ; i < books.getLength () ; i ++ )
     50                 {
     51                     Node book = books.item (i) ;
     52                     if (book.getNodeName ().equals (text))
     53                     {
     54                         infotext = book.getFirstChild ().getNodeValue () ;
     55                         //System.out.println(infotext);
     56                         return infotext;
     57                     }
     58                     if (book.getNodeType () == Node.ELEMENT_NODE)
     59                     {
     60                         for (Node node = book.getFirstChild () ; node != null ; node = node.getNextSibling ())
     61                         {
     62                             if (node.getNodeType () == Node.ELEMENT_NODE)
     63                             {
     64                                 if (node.getNodeName ().equals (text))
     65                                 {
     66                                     infotext = node.getFirstChild ().getNodeValue () ;
     67 //System.out.println (infotext) ;
     68                                     return infotext ;
     69                                 }
     70                             }
     71                         }
     72                     }
     73                 }
     74             }
     75         }
     76         catch (ParserConfigurationException e)
     77         {
     78             e.printStackTrace () ;
     79         }
     80         catch (FileNotFoundException e)
     81         {
     82             e.printStackTrace () ;
     83         }
     84         catch (SAXException e)
     85         {
     86             e.printStackTrace () ;
     87         }
     88         catch (IOException e)
     89         {
     90             e.printStackTrace () ;
     91         }
     92         return infotext ;
     93     }
     94 //    public static void main(String args[])
     95 //    {
     96 //        //為了打包 測試路徑
     97 //        JFrame frame = new JFrame ("test");
     98 //        frame.setLayout (null);
     99 //        frame.setSize (200,300);
    100 //        JLabel label = new JLabel(parse("name"));
    101 //        label.setBounds (0,0,50,100);
    102 //        frame.add (label);
    103 //        frame.setVisible (true);
    104 //        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    105 //        System.out.println (parse("name"));
    106 //    }
    107 }
    108 


    posted on 2007-02-06 17:12 -274°C 閱讀(978) 評論(0)  編輯  收藏 所屬分類: JAVAXML

    常用鏈接

    留言簿(21)

    隨筆分類(265)

    隨筆檔案(242)

    相冊

    JAVA網站

    關注的Blog

    搜索

    •  

    積分與排名

    • 積分 - 914354
    • 排名 - 40

    最新評論

    主站蜘蛛池模板: 全免费A级毛片免费看网站| 久久99精品视免费看| 在线免费观看中文字幕| 亚洲精品不卡视频| 久久99热精品免费观看牛牛| 亚洲精品无码MV在线观看| h片在线观看免费| 国产亚洲精aa成人网站| 中文字幕免费观看全部电影| 亚洲一区二区三区乱码A| 一个人晚上在线观看的免费视频 | 亚洲an日韩专区在线| 国产乱码免费卡1卡二卡3卡| 亚洲国产成人久久综合一区| 亚洲免费福利在线视频| 亚洲小说图区综合在线| 国产免费午夜a无码v视频| 一级毛片不卡免费看老司机| 亚洲人成图片小说网站| 91精品全国免费观看含羞草| 亚洲一区无码中文字幕乱码| 免费观看的毛片手机视频| 免费人成网站永久| 九月丁香婷婷亚洲综合色| 2019中文字幕在线电影免费| 亚洲熟妇无码AV不卡在线播放 | 中文字幕版免费电影网站| 亚洲a在线视频视频| 免费国产作爱视频网站| 亚洲精品色在线网站| 成人亚洲性情网站WWW在线观看| 两个人看的www高清免费视频 | 免费国产成人高清在线观看网站| 亚洲欧美自偷自拍另类视| 亚洲国产精品丝袜在线观看| 久久青草免费91线频观看不卡| 亚洲免费观看在线视频| 亚洲国产精品专区在线观看 | 精品多毛少妇人妻AV免费久久| 午夜亚洲国产理论秋霞| 男女交性永久免费视频播放|