Java and XML are gold combinations. There are many articles on the Internet. XML is exchanged as an e-commerce, which already has an irreplaceable role, but in the usual system development, we do not necessarily use data exchange, is it unable to use XML?
Of course, there is already a new trend now, both the Java program has started using the XML format, which is used to use the INI format similar to Windows. (Java also has the profile of Properties like this property). Use XML as Java's profile has a lot of benefits, from Tomcat installation profiles and J2EE configuration files, we have seen XML's universal applications, let us follow the popular trend with XML arms.
Now how is the key to read an XML configuration file? There are several XML parsers: there are main DOMs and SAX, which are many online articles.
In the XML project group of Apache, there is currently Xerces Xalan Cocoon's project.tomcat that develops XML related technologies itself uses Sun's JAXP, and its Xerces parser in XSL Taglib Project.
Ok, it is a more annoying theoretical issue, or cut into the reading of XML profiles.
In our program, there are usually some variables determined according to the host environment. For example, the database access username and password, different hosts may set differently. As long as you change the XML configuration file, you can run it normally.
The above Myenv.xml configuration file is usually placed in the Tomcat's web-inf / class.
We compile a Java program to read directly to extract DBHOST DBUSER DBPassword to other programs to access databases.
Currently using SAX comparison, the main difference between DOM is that SAX is a row and a line of reading XML files for analysis. It is suitable for more large files. Dom is a one-time read memory, obviously cannot deal with large files. Here we use SAX resolution, due to SAX The parser is constantly developing, there are many articles on the Internet to be older versions. If you use JDK1.4, you can refer to the use of SAX to process XML documents. The program is based on its improvement and has been commissioned.
Java programs read on my aenv.xml above:
Import org.xml.sax.attributes; import org.xml.sax.helpers.defaulthandler; import org.xml.sax.saxexception; import java.util.properties; // The benefits of using DefaultHandler do not have to display all methods, public class ConfigParser extends DefaultHandler {Properties for storing values defining a private Properties props dbhost dbuser dbpassword of; private String currentSet; private String currentName; private StringBuffer currentValue = new StringBuffer (); // initialize builder propspublic ConfigParser () {this.props = New Properties ();} public property getProps () {return this.props;} // Define how to start resolving elements. Here is the name of the name XXX in
Now we have extracted DBHOST DBUSER DBPassword's value localhost sqlname username password. But this is just in the parser, and our programs cannot be accessed. You need to prepare a program again.
import java.util.Properties; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import java.net.URL; public class ParseXML {// Properties defined for storing a value of private dbhost dbuser dbpassword Properties props; // propspublic herein Properties getProps () {return this.props;} public void parse (String filename) throws Exception {// parser object of our ConfigParser handler = new ConfigParser (); // Get SAX factory object SAXParserFactory factory = SAXParserFactory.newInstance (); factory.setNamespaceAware (false); factory.setValidating (false); // get SAX parsing SAXParser parser = factory.newSAXParser (); // get the configuration file myenv.xml directory. In the Web-INF / CLASSES //, BeansconStants is used to store configuration information in the XML file, which can replace or define URL Confurl = Beansconstants.class.getClassLoader (). getresource (filename); try { // Link the parser and parsing object myenv.xml, start parsing Parser.Parse (Confurl.Tostring (), Handler); // After obtaining the successful attribute after the resolution, other applications can be called as long as the PrOPs of this program can be Extract the attribute name and value props = handler.getProps ();} finally {factory = null; Parser = null; handler = null;}}} Since our XML file is the simplest form using the ultimate form, the parser is relatively simple But this is enough to deal with our profile.
To determine the advanced nature of a program system, let's take a look at his profile. If you still use the old suit's XXX = 123 like. Ini files, we may smile, he is outdated .....