对两种情况,这个文件不需要修改:
 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     ////定义一个Properties 用来存放 dbhost dbuser dbpassword的值
 9     private Properties props;
10     
11     private String currentSet;
12     private String currentName;
13     private StringBuffer currentValue = new StringBuffer();
14     //构建器初始化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>结束后,将之前的名称和值一一对应保存在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 

这个文件中注释 与注释之间的是对不同情况的对比:
 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     //定义一个Properties 用来存放标签值
 9     private Properties props;
10     public Properties getProps()
11     {
12         return this.props;
13     }
14     public void parse(String filename) throws Exception
15     {
16         //将解析器对象化
17       try
18         {
19           ConfigParser handler = new ConfigParser();
20         //获取SAX工厂对象
21         SAXParserFactory factory = SAXParserFactory.newInstance();
22         factory.setNamespaceAware(false);
23         factory.setValidating(false);
24         //获取SAX解析
25         SAXParser parser = factory.newSAXParser();
26      
27 /////////////////////////////////////////////////////////////////////////////
28 //对字符串解析:
29 //            InputSource is = new InputSource ();
30 //            StringReader xmlStr = new StringReader (filename);
31 //            is.setCharacterStream (xmlStr);
32 //            parser.parse (is,handler);
33  ////////////////////////////////////////////////////////////////////////////    
34             
35 ////////////////////////////////////////////////////////////////////////////
36 //  对文件解析:
37         URL confURL = getClass().getResource(filename);
38         if(confURL == null) System.out.println("error");
39         
40             //将解析器和解析对象xml联系起来,开始解析
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 


测试程序:
 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();  //解析字符串用这个方法
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 阅读(8631) 评论(5)  编辑  收藏 所属分类: JAVAXML


FeedBack:
# re: 解析XML字符串与xml文件
2007-02-07 15:22 | BeanSoft
感谢分享, 共同进步!  回复  更多评论
  
# 感谢!
2008-09-19 21:32 | 398589389
谢谢你,好东西啊  回复  更多评论
  
# re: 解析XML字符串与xml文件
2009-09-11 12:57 | 香蕉果果
想问一下,如果想取标签的属性值又该怎么取呢,比如
<result status="0" message="消息发送成功,发送了1条消息"><serial_id count="1"><id value="13333333" sp_name="" /></serial_id></result>

比如我要取里面status的值  回复  更多评论
  
# re: 解析XML字符串与xml文件
2010-08-27 18:04 | jiakly
谢谢!  回复  更多评论
  
# re: 解析XML字符串与xml文件
2010-08-27 18:13 | jiakly
谢谢你提供的代码
怎么只能读一个节点下的啊,都给一样的节点就只读最后一个.
例如:
<?xml version='1.0' encoding='GB2312'?>
<response>
<vo> <a>1</a> </vo>
<vo><a>2</a> </vo>
</response>  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(21)

随笔分类(265)

随笔档案(242)

相册

JAVA网站

关注的Blog

搜索

  •  

积分与排名

  • 积分 - 909113
  • 排名 - 40

最新评论