How to use dom4j parsing XML file to get data in a Java project

Source: Internet
Author: User
Tags xpath

In the past, learning. NET often encounters the use of configuration files to solve some of the data that needs to be changed frequently in the project. For example, a database connection string. This time when the configuration file is read. We will generally use a minefield, through this class for direct reading, it can be said that this is directly packaged in the program, including configuration files written in the format and so on. Well, suppose. Want to write some other data in the configuration file, write it directly in the format of the XML file. How do you read the data in the XML file in the background at this time? Here, dom4j is used to parse the XML data in the XML file that connects to the Oracle database.

Before using, you should first copy the Dom4j-1.6.1.jar file package to your project in your own project.

1. Create an XML file

<?

XML version= "1.0" encoding= "UTF-8"?

><config><db-info><driver-name>oracle.jdbc.driver.oracledriver</driver-name><url >jdbc:oracle:thin: @localhost: 1521:bjpowernode</url><user-name>drp</user-name><password >drp</password></db-info></config>


2. Create a new console application to read the XML file

Detailed implementations such as the following:

Import Java.io.inputstream;import org.dom4j.document;import org.dom4j.documentexception;import org.dom4j.Element; Import Org.dom4j.io.SAXReader;
public static void Main (string[] args) {Saxreader reader=new saxreader ();//By obtaining the current thread, and then obtaining the current class loader, to obtain the corresponding resource InputStream in =thread.currentthread (). Getcontextclassloader (). getResourceAsStream ("Sys-config.xml"); try {// Read will return a Document object document Doc=reader.read (in);//You can also read the file Reader.read directly (new file ("Sys-config.xml")// Gets the node element of the XML file from the document object, returns element, and gets the element drivernameelt= (element) Doc.selectobject ("/config/db-info/") through the XPath path. Driver-name "); Element urlelt= (Element) Doc.selectobject ("/config/db-info/url"); Element usernameelt= (Element) Doc.selectobject ("/config/db-info/user-name"); Element passwordelt= (Element) Doc.selectobject ("/config/db-info/password");//Gets the value in the tag string drivername= Drivernameelt.getstringvalue (); String Url=urlelt.getstringvalue (); String Username=usernameelt.getstringvalue (); String Password=passwordelt.getstringvalue ();      System.out.println (drivername);
              System.out.println (URL);
                      SYSTEM.OUT.PRINTLN (username);

              SYSTEM.OUT.PRINTLN (password);
          } catch (Documentexception e) {   }
}

3. View the effect:

4, the knowledge point of contactIn the process of parsing XML using DOM4J, I used the XPath path when I fetched the node. This gets the amount each node is an element type. Therefore, we can get to the node after the operation of the node, additions and deletions and other operations.

Here the operation of the XML file, similar to the operation of the HTML document, the entire file is in fact a document type of documents, each element is an element type of elements, and its very many operations in fact, and manipulate the elements in the HTML document is similar.

How to use dom4j parsing XML file to get data in a Java project

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.