Reading XML configuration files (sax)

Source: Internet
Author: User


In a recent MIS project, to avoid hard coding, I need to write some configuration information in a configuration file. Considering that it is a J2EE project, the J2EE configuration file
It seems that they are all xml files. Is it a little outdated to use traditional INI files?
OK. use xml as the configuration file.
My configuration file reportenv. xml is as follows, which is relatively simple:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Reportenv>
<Datasource>
<Username> sqlname </username>
<Password> password </password>
</Datasource>
</Reportenv>
The question is, what can I use to read configuration information?
Currently, dom4j and sax are popular. I used to use dom4j all the time. However, weblogic workshop comes with sax. I don't want to introduce any more packages, so it's just sax.
Step 1: ConfigParser. java
/*
* Create Date: 2005-6-13
* Create By: Banqiao Renren
* Purpose: xml configuration file property Reader
*/
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 {
/// Define a Properties to store the property value
Private Properties props;
Private String currentSet;
Private String currentName;
Private StringBuffer currentValue = new StringBuffer ();
// The Builder initializes props.
Public ConfigParser (){
This. props = new Properties ();
}
Public Properties getProps (){
Return this. props;
}
// Define the method for starting element parsing. Here, the name xxx in <xxx> is extracted.
Public void startElement (String uri, String localName, String qName, Attributes attributes)
Throws SAXException {
CurrentValue. delete (0, currentValue. length ());

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.