Read processing of XML configuration files

xiaoxiao2021-03-19  229

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.

Program code: localhost SQLNAME username password

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:

Program code: import org.xml.sax.attribute; 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 the method, 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 prof.Props;} // Define how to start resolving elements. Here is the name XXX in to extract .public void StartElement (String Uri , String localname, string qname, attributes) throws attribute {currentValue.delete (0, currentValue.Length (); this.currentname = QNAME;} // This is the value between To CurrentValuepublic Void Characters (char [] ch, int start, int length) throws saxexception {currentValue.Append (ch, start, length);} // After encountering , the previous name and value One correspondence in PUBLIC VOID Endelement (String Uri, String Localname, String Qname) throw SAXException {Props.Put (Qname.tolowerCase (), currentValue.toString (). Trim ());}} The above resolution is simpler? In fact, the resolution XML is so simple.

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.

Program code: 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 dbhost dbuser dbpassword value private Properties props; // here propspublic Properties getProps () {return this.props;} public void parse (String filename) throws Exception {// parser object of our ConfigParser handler = new ConfigParser (); / / SAX obtain a factory object SAXParserFactory factory = SAXParserFactory.newInstance (); factory.setNamespaceAware (false); factory.setValidating (false); // Get SAX parsing SAXParser parser = factory.newSAXParser (); // get the profile myenv.xml The directory. Tomcat is in the Web-INF / CLASS / /, BeansconStants is the class used to configure 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 (confur.tostring (), handler); // After obtaining the successful properties after the resolution, other applications will call this procedure. PROPS can 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 it, This is relatively simple, but this is enough to deal with our profile.

Judging the advanced nature of a program system, let's take a look at his profile, if you still use the old suit XXX = 123 like .ini file,

We may smile slightly, he is out of date ...

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

New Post(0)