XML設定檔的讀取(sax)

來源:互聯網
上載者:User

在最近的一個MIS項目中,為了避免寫入程式碼,我需要把一些配置資訊寫在一個設定檔中.考慮到是J2EE項目,J2EE的設定檔
好像都是xml檔案了,再用傳統ini檔案是不是有點落伍了?
ok,就用xml做設定檔吧.
我的設定檔reportenv.xml如下,比較簡單:
<?xml version="1.0" encoding="utf-8"?>
<reportenv>
<datasource>
<username>sqlname</username>
<password>password</password>
</datasource>
</reportenv>
現在的問題是我用什麼來讀取配置資訊?
現在流行的是dom4j和sax,我以前一直用dom4j.可是weblogic workshop內建的是sax,我又不想再引入包了,於是就是sax吧.
第一步:ConfigParser.java
/*
 * Create Date: 2005-6-13
 * Create By: 板橋裡人
 * purpose:xml設定檔屬性讀取器
 */
package com.infoearth.report;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import java.util.Properties;
public class ConfigParser extends DefaultHandler {
    ////定義一個Properties 用來存放屬性值
    private Properties props;
    private String currentSet;
    private String currentName;
    private StringBuffer currentValue = new StringBuffer();
    //構建器初始化props
    public ConfigParser() {
        this.props = new Properties();
        }
    public Properties getProps() {
        return this.props;
        }
    //定義開始解析元素的方法. 這裡是將<xxx>中的名稱xxx提取出來.
    public void startElement(String uri, String localName, String qName, Attributes attributes)
    throws SAXException {
        currentValue.delete(0, currentValue.length());

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.