對(duì)兩種情況,這個(gè)文件不需要修改:
           1 import org.xml.sax.Attributes;
           2 import org.xml.sax.helpers.DefaultHandler;
           3 import org.xml.sax.SAXException;
           4 import java.util.Properties;
           5 
           6 public class ConfigParser extends DefaultHandler
           7 {
           8     ////定義一個(gè)Properties 用來存放 dbhost dbuser dbpassword的值
           9     private Properties props;
          10     
          11     private String currentSet;
          12     private String currentName;
          13     private StringBuffer currentValue = new StringBuffer();
          14     //構(gòu)建器初始化props
          15     public ConfigParser()
          16     {
          17         this.props = new Properties();
          18     }
          19     public Properties getProps()
          20     {
          21         return this.props;
          22     }
          23     
          24     
          25     //定義開始解析元素的方法. 這里是將<xxx>中的名稱xxx提取出來.
          26     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
          27     {
          28         currentValue.delete(0, currentValue.length());
          29         this.currentName =qName;
          30     }
          31     //這里是將<xxx></xxx>之間的值加入到currentValue
          32     public void characters(char[] ch, int start, int length) throws SAXException
          33     {
          34         currentValue.append(ch, start, length);
          35     }
          36     //在遇到</xxx>結(jié)束后,將之前的名稱和值一一對(duì)應(yīng)保存在props中
          37     public void endElement(String uri, String localName, String qName) throws SAXException
          38     {
          39         props.put(qName.toLowerCase(), currentValue.toString().trim());
          40 //System.out.println(qName.toLowerCase() + " " + currentValue.toString().trim());
          41     }
          42 }
          43 

          這個(gè)文件中注釋 與注釋之間的是對(duì)不同情況的對(duì)比:
           1 import java.net.URL;
           2 import java.util.Properties;
           3 import javax.xml.parsers.SAXParser;
           4 import javax.xml.parsers.SAXParserFactory;
           5 
           6 public class ParseXML
           7 {
           8     //定義一個(gè)Properties 用來存放標(biāo)簽值
           9     private Properties props;
          10     public Properties getProps()
          11     {
          12         return this.props;
          13     }
          14     public void parse(String filename) throws Exception
          15     {
          16         //將解析器對(duì)象化
          17       try
          18         {
          19           ConfigParser handler = new ConfigParser();
          20         //獲取SAX工廠對(duì)象
          21         SAXParserFactory factory = SAXParserFactory.newInstance();
          22         factory.setNamespaceAware(false);
          23         factory.setValidating(false);
          24         //獲取SAX解析
          25         SAXParser parser = factory.newSAXParser();
          26      
          27 /////////////////////////////////////////////////////////////////////////////
          28 //對(duì)字符串解析:
          29 //            InputSource is = new InputSource ();
          30 //            StringReader xmlStr = new StringReader (filename);
          31 //            is.setCharacterStream (xmlStr);
          32 //            parser.parse (is,handler);
          33  ////////////////////////////////////////////////////////////////////////////    
          34             
          35 ////////////////////////////////////////////////////////////////////////////
          36 //  對(duì)文件解析:
          37         URL confURL = getClass().getResource(filename);
          38         if(confURL == null) System.out.println("error");
          39         
          40             //將解析器和解析對(duì)象xml聯(lián)系起來,開始解析
          41             parser.parse(confURL.toString(), handler);
          42 /////////////////////////////////////////////////////////////////////////
          43           props = handler.getProps();
          44         }
          45      catch(Exception e)
          46      {
          47          System.out.println (e.toString ());
          48      }
          49     }
          50 }
          51 
          52 


          測(cè)試程序:
           1 import java.util.*;
           2 
           3 public class Main
           4 {
           5     static ParseXML px = new ParseXML ();
           6     public static void main (String[] args)
           7     {
           8         //load_properties ();   //解析xml文件
           9         load_properStr();  //解析字符串用這個(gè)方法
          10         String  issuccessful = (String) getObject ("result");
          11         String  objRequestID =  (String) getObject ("msg");
          12         System.out.println ("issuccessful ::"+issuccessful);
          13         System.out.println ("objRequestID ::"+objRequestID);
          14         
          15     }
          16     
          17     public static void load_properStr ()
          18     {
          19         
          20         String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
          21                 "<response>"+
          22                 "<result>0</result>"+
          23                 "<msg>47F42A2D578</msg>"+
          24                 "</response>";
          25         try
          26         {
          27             px.parse (str);
          28         }
          29         catch (Exception ex)
          30         {
          31             ex.printStackTrace ();
          32         }
          33         
          34     }
          35     public static void  load_properties ()
          36     {
          37         try
          38         {
          39             px.parse ("/properties.xml");
          40         }
          41         catch (Exception ex)
          42         {
          43             ex.printStackTrace ();
          44         }
          45     }
          46     public static Object getObject (String keyname)
          47     {
          48         return px.getProps ().getProperty (keyname);
          49     }
          50 }
          51 




          posted on 2007-02-07 10:53 -274°C 閱讀(8647) 評(píng)論(5)  編輯  收藏 所屬分類: JAVAXML


          FeedBack:
          # re: 解析XML字符串與xml文件
          2007-02-07 15:22 | BeanSoft
          感謝分享, 共同進(jìn)步!  回復(fù)  更多評(píng)論
            
          # 感謝!
          2008-09-19 21:32 | 398589389
          謝謝你,好東西啊  回復(fù)  更多評(píng)論
            
          # re: 解析XML字符串與xml文件
          2009-09-11 12:57 | 香蕉果果
          想問一下,如果想取標(biāo)簽的屬性值又該怎么取呢,比如
          <result status="0" message="消息發(fā)送成功,發(fā)送了1條消息"><serial_id count="1"><id value="13333333" sp_name="" /></serial_id></result>

          比如我要取里面status的值  回復(fù)  更多評(píng)論
            
          # re: 解析XML字符串與xml文件
          2010-08-27 18:04 | jiakly
          # re: 解析XML字符串與xml文件
          2010-08-27 18:13 | jiakly
          謝謝你提供的代碼
          怎么只能讀一個(gè)節(jié)點(diǎn)下的啊,都給一樣的節(jié)點(diǎn)就只讀最后一個(gè).
          例如:
          <?xml version='1.0' encoding='GB2312'?>
          <response>
          <vo> <a>1</a> </vo>
          <vo><a>2</a> </vo>
          </response>  回復(fù)  更多評(píng)論
            

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊(cè)

          JAVA網(wǎng)站

          關(guān)注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914107
          • 排名 - 40

          最新評(píng)論

          主站蜘蛛池模板: 海南省| 临安市| 榆中县| 揭西县| 柯坪县| 惠州市| 湖州市| 正蓝旗| 松潘县| 揭东县| 讷河市| 堆龙德庆县| 永仁县| 永济市| 团风县| 河源市| 博乐市| 大宁县| 大足县| 芦山县| 大姚县| 乌鲁木齐县| 临江市| 胶南市| 海阳市| 双牌县| 三门县| 大化| 北京市| 巴塘县| 青岛市| 苗栗市| 泾阳县| 浙江省| 临洮县| 邹平县| 永靖县| 庆安县| 雅安市| 横山县| 牙克石市|