Reading xml configuration files (SAX)

Source: Internet
Author: User
Tags xml attribute

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)
Throws saxexception {
Currentvalue. Delete (0, currentvalue. Length ());
This. currentname = QNAME;
}

// Add the value between <XXX> </xxx> to currentvalue.
Public void characters (char [] CH, int start, int length) throws saxexception {
Currentvalue. append (CH, start, length );
}

// Save the previous names and values in props one by one after </xxx>
Public void endelement (string Uri, string localname, string QNAME) throws saxexception {
Props. Put (QNAME. tolowercase (), currentvalue. tostring (). Trim ());
}

}

Step 2: parsexml. Java
/*
* Create Date: 2005-6-13
* Create by: Li chunlei, Banqiao Renren, modified
* Purpose: xml configuration file property reader (common ),
*/
 
Package com. infoearth. Report;

Import java. util. properties;
Import javax. xml. parsers. saxparser;
Import javax. xml. parsers. saxparserfactory;
Import java.net. url;

Public class parsexml {
// Define a properties to store the property value
Private Properties props;

Public properties getprops (){
Return this. Props;
}

Public void parse (string filename) throws exception {
// Visualize our parser
Configparser handler = new configparser ();
// Obtain the sax factory object
Saxparserfactory factory = saxparserfactory. newinstance ();
Factory. setnamespaceaware (false );
Factory. setvalidating (false );
// Obtain the sax Parsing
Saxparser parser = factory. newsaxparser ();
Try {
// Associate the parser with the parsing object XML to start Parsing
Parser. parse (filename, Handler );
// Obtain the attributes after successful resolution
Props = handler. getprops ();
} Finally {
Factory = NULL;
Parser = NULL;
Handler = NULL;
}
}
}
Step 3: readconfigxml. Java
/*
* Create Date: 2005-6-13
* Create by: Li chunlei
* Purpose: xml configuration file property Reader
*/

Package com. infoearth. Report;

Import java. util. properties;

Public class readconfigxml
{
Private Properties props;

Public readconfigxml (string URL ){
Parsexml myread = new parsexml ();
Try {
Myread. parse (URL );
Props = new properties ();
Props = myread. getprops ();
} Catch (exception e ){
E. printstacktrace ();
}
}
Public String GetUserName (){
Return props. getproperty ("username ");
}
Public String GetPassword (){
Return props. getproperty ("password ");
}

}

OK. The read process is as follows:
Readconfigxml xmlread = new readconfigxml ("reportenv. xml ");
String username = xmlread. GetUserName ();
String Password = xmlread. GetPassword ();

The first two classes implement arbitrary reading of the XML document attribute settings. As long as the XML Attribute value is read to the property, you only need to extract it from the property.
The third class is written for my XML files. It seems a little redundant. Haha. In fact, it is hard to say. Because I don't want to change the previous program architecture too much, I just want to add a snake.
Enough.

In addition, thanks to J Dao and Banqiao people.

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.