解析XML字符串与xml文件
对两种情况,这个文件不需要修改:
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
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
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
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