XML configuration file read (sax)

Source: Internet
Author: User
Tags date define config stringbuffer
Xml

In a recent MIS project, in order to avoid hard coding, I need to write some configuration information in a configuration file. Consider the Java EE project, Java EE configuration file
Seems to be all XML files, and then use the traditional INI file is not a bit outdated?
OK, just use XML as a configuration file.
My profile reportenv.xml is as follows, relatively simple:

<?xml version= "1.0" encoding= "Utf-8"?>
<reportenv>
<datasource>
<username>sqlname</username>
<password>password</password>
</datasource>
</reportenv>

Now the question is, what do I use to read the configuration information?
Now it is dom4j and sax that I used to use DOM4J. But WebLogic workshop comes with sax, and I don't want to introduce the package anymore, so it's sax.
First step: Configparser.java
/*
* Create date:2005-6-13
* Create by: Banqiao people
* Purpose:xml Config 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 property to hold the attribute value
Private Properties props;

Private String Currentset;
Private String Currentname;
Private StringBuffer CurrentValue = new StringBuffer ();

Builder Initialization Props
Public Configparser () {

This.props = new Properties ();
}

Public Properties GetProps () {
return this.props;
}

Defines a method that begins parsing an element. This is to extract the name xxx in <xxx>.
public void Startelement (string uri, String localname, String qName, Attributes Attributes)
Throws Saxexception {
Currentvalue.delete (0, Currentvalue.length ());
This.currentname =qname;
}

This is to add the value between <xxx></xxx> to CurrentValue
public void characters (char[] ch, int start, int length) throws Saxexception {
Currentvalue.append (CH, start, length);
}

After you encounter the </xxx> end, save the previous name and value one by one in props
public void EndElement (string uri, String localname, String qName) throws Saxexception {
Props.put (Qname.tolowercase (), currentvalue.tostring (). Trim ());
}

}

Step Two: Parsexml.java
/*
* Create date:2005-6-13
* Create by: Banqiao people Li Chunlei modified
* Purpose:xml profile Property Reader (generic),
*/

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 property to hold the attribute value
Private Properties props;

Public Properties GetProps () {
return this.props;
}

public void Parse (String filename) throws Exception {
The object of our parser
Configparser handler = new Configparser ();
Get Sax Factory Objects
SAXParserFactory factory = Saxparserfactory.newinstance ();
Factory.setnamespaceaware (FALSE);
Factory.setvalidating (FALSE);
Get Sax parsing
SAXParser parser = Factory.newsaxparser ();
try{
To associate the parser with parsing object XML, start parsing
Parser.parse (filename, handler);
Gets the properties after the resolution is successful
Props = Handler.getprops ();
}finally{
Factory=null;
Parser=null;
Handler=null;
}
}
}
Step Three: Readconfigxml.java
/*
* Create date:2005-6-13
* Create by: Li Chunlei
* Purpose:xml Config 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, it's done, the reading is as follows:
Readconfigxml xmlread = new Readconfigxml ("Reportenv.xml");
String username = xmlread.getusername ();
String password = Xmlread.getpassword ();

The first two classes implement arbitrary reads of the XML document property settings. As long as the attribute value of XML is read in the property, you can only extract it from the properties.
The third class, which I wrote about my XML file, seems a bit redundant. Oh, there is a reason for this. Because I don't want to change the previous program structure too much, I draw snake Tim
Enough for a moment.

In addition, thanks to J Road, thanks to 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.