解析XML

  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 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值