Read processing of XML configuration files in Java

Source: Internet
Author: User
Tags tomcat stringbuffer

Java and XML is a combination of gold, there are many articles on the Internet, XML as a data exchange in E-commerce, has its irreplaceable role, but in peacetime system development, we do not necessarily have to use data exchange, is not the use of XML?

Of course not, now there is a new trend, the Java program's configuration files are beginning to use XML format, used to be in the same way as the Windows INI format (in Java, there are propertiesy such a class specialized in such a property configuration file). There are many benefits to using XML as a Java configuration file, and we've seen the universal application of XML from the Tomcat installation profile and the EE configuration file, so we're also arming ourselves with the popular trends with XML.

Now the key is how to read the XML configuration file? There are several types of XML parsers: The main DOM and sax, and in the Apache XML Project group, there are currently xerces Xalan Cocoon several XML-related technologies Project.tomcat themselves using sun Jaxp, and their XSL Taglib The Xerces parser is used in project.

OK, all of these are annoying theoretical issues, or quickly cut into the XML configuration file read it.

In our program, there are usually some variables that are determined according to the host environment. For example, the database access username and password, different hosts may be set differently. As long as you change the XML configuration file, you can run it correctly.

﹤myenv﹥
﹤datasource﹥
﹤dbhost﹥localhost﹤/dbhost﹥
﹤dbname﹥sqlname﹤/dbname﹥
﹤dbuser﹥username﹤/dbuser﹥
﹤dbpassword﹥password﹤/dbpassword﹥
﹤/datasource﹥
﹤/myenv﹥

The above myenv.xml configuration file is typically placed under Tomcat's web-inf/classes directory.

We compiled a Java program to read directly, Dbhost Dbuser Dbpassword extracted for other programs to access the database.

Currently using Sax is much more, the main difference with DOM is that sax is a row to read XML files for analysis, suitable for larger files, Dom is a one-time read into memory, obviously can not deal with large files. Here we use Sax parsing, and as the SAX parser continues to evolve, there are a number of articles on the web that are aimed at older versions. If you use JDK1.4, you can refer to the article using sax to process XML documents. The program here is based on its improvement and has been debugged in practice.

Java program to read above myenv.xml:

import org.xml.sax.Attributes;
import Org.xml.sax.helpers.DefaultHandler;
Import Org.xml.sax.SAXException;
Import java.util.Properties;
//The benefit of using DefaultHandler
is that you do not have to display all the methods,
public class Configparser
extends DefaultHandler
{
//// Define a properties to hold the
Dbhost dbuser dbpassword value
Private Properties props;
Private String currentset;
Priva Te String currentname;
Private StringBuffer
CurrentValue = new StringBuffer ();
//builder Initialization Props
Public configparser ()
{
th Is.props = new Properties ();
}
Public Properties getprops ()
{
return this.props;
} The
//defines the method that begins parsing the element.
This is to extract the name xxx in ﹤xxx﹥.
public void Startelement
(string uri, String localname,
String qName, Attributes Attributes)
throws Saxex Ception
{
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 ﹤/xxx﹥ is encountered,
the previous name and value one by one are saved in props
public void endelement (string uri,
string localname, String qName
throws Saxexception
{
Props.put qname.tolowercase (),
currentvalue.tostring (). Trim ();
}
}

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.