解析XML字符串与xml文件

对两种情况,这个文件不需要修改:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->  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 

这个文件中注释 与注释之间的是对不同情况的对比:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->  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 


测试程序:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->  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 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我们为什么要在LabVIEW中使用XML文件?①它是许多服务器数据传输的基本格式,在网络编程中有很大的几率要使用它。②它比ini文件功能强大,它可以编写复杂逻辑关系的数据配置文件,类似一个微型的数据库文件。。。。。。 感谢微软提供了OLE技术,使得不同程序之间可以通过预先指定的接口互相调用。微软提供了MSXML2.0, 4.0 及 6.0动态链接库来支援开发人员读写XML文件。 而LabVIEW正可以通过OLE自动化接口,调用MSXML*.dll动态链接库,完成对XML文件的操作。如果你的系统没有安装MSXML*.dll,可以去网上下载并安装就可以了。论坛中有许多朋友已经使用该方式去读写EXCEL, WORD,ACCESS。 有了OLE接口是不是就能很方便的读写XML文件了呢?不是,因为其中的操作是通过引用(Reference)来完成的,使用引用并不是免费的午餐,你必须时刻提防内存泄露,如果你忘记释放它,那么你就会在任务管理器中看到:你的程序内存消耗在不断的增长。 更要命的是XML文件是以树的方式构成的,从上往下分大致有:Document, root node, node List, node,而node中又包含parent node, child node,你的程序怎么才能组织好对它的调用?想想头都大了,是不是? 还好我们还有面向对象编程,把以上的东西都归下类,发现XML文件就是由Document, node list 和 node组成的,其他的都是根据这三个类派生出来的。那么就定义三个类,分别是CDoc, CNodeList 和 CNode由他们负责来完成对XML文件的操作。更方便的是,对于引用(Reference)的管理也都在这三个类当中完成,这有效地减轻了程序的复杂度。 附件中的程序是用LabVIEW8.5开发的,其中包含了一份Sample.xml文件,供大家测试。 本程序是调用MSXML4.0接口来完成的,如果你系统里没有安装,那么将不能运行该程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值