Read processing of XML configuration files

xiaoxiao2021-03-06  43

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 configuration files and J2EE configuration files, we have seen XML's universal applications, let us follow the popular trend with XML arms. Now how is the XML configuration file Have several XML parsers: mainly DOM and SAX, these differences online article introduction. In the XML project group of Apache, there are currently Xerces Xalan Cocoon's development XML related technology project.tomcat itself is Sun JAXP, and its Xerces parser in our XSL Taglib Project. Ok, it is a more annoying theoretical issue, or cut into the reading of XML configuration files. In our program, there are usually some mainframes. The environment determined variables. For example, the database access username and password, different hosts may set differently. Just change the XML configuration file, you can run it normally. localhost Sqlname < / dbname> Username password above this Myenv.xml configuration file is usually placed in Tomcat's web-inf / class. We are prepared A Java program reads directly to extract DBHOST DBUSER DBPassword to other programs to access the database. Currently using SAX comparison, the main difference with the DOM is SAX is a line of reading XML files to analyze, suitable for more big files, DOM is Read memory one-time, obviously cannot deal with big files. Here we use SAX resolution, because the SAX parser is constantly developing, there are many articles on the Internet to be old version. If you use JDK1.4, you can refer to using SAX processing XML Documentation One article This program is based on its improvement and has been commissioned. Java programs read on the above myenv.xml: import org.xml.sax.attributes; import org.xml.sax.helpers.defaulthandler; import Org.xml.sax.saxexce ption; import java.util.Properties; // use DefaultHandler benefit is not necessarily 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 ();}; // define the start method of resolving element public Properties getProps () {return this.props} here is the name xxx is extracted .public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException {currentValue.delete (0, currentValue.length ()); this.currentName = qname;} / / This is <

转载请注明原文地址:https://www.9cbs.com/read-67140.html

New Post(0)