(C) a simple path dom4j + Xpath expression acquires node element content and attribute values

1, the leader packet

 

 2, create sys-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <database-info>
        <driver-name>com.mysql.jdbc.Driver</driver-name>
        <url>jdbc:mysql://localhost:3306/sys?serverTimezone=GMT%2B8</url>
        <user>root</user>
        <password>123</password>
    </database-info>
</config>

3, dom4j + Xpath expressions obtain simple path node element content and attribute values

package com.zda.xml;


import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class SysConfigParser {

    / ** 
     * @param args
      * / 
    public  static  void main (String [] args) {
         the try {
             // create a parser 
            SAXReader Reader = new new SAXReader ();
             // by the read method parser configuration file read into memory in generating one object tree Dcoumente] [org.dom4j 
            the Document Document reader.Read = ( "the conf / SYS-the config.xml" );
             // Driver-node element path name: config -> database-info - > driver- name
             // Driver element xpath-node path name: / config / database-info / driver-name
            = DriverNameElt the Element (the Element) document.selectSingleNode ( "/ config / Database-info / Driver-name" );
             // Get driverNameElt node element object text 
            String driverName = driverNameElt.getStringValue ();
            System.out.println(driverName);
            
            // path node element url: config -> Database-info -> url
             // url xpath node element path: / config / Database-info / url
             // xpath node element path url: config // url
             // url xpath path node elements: // URL 
            the element urlElt = (the element) document.selectSingleNode ( "/ config URL //" );
            String url = urlElt.getStringValue();
            System.out.println(url);
            
            // path user node elements: config -> Database-info -> user
             @ XPath user node element path: / config / Database-info / user
             // XPath path user node elements: config // user
             @ user xpath path node elements: // User 
            the element userElt = (the element) document.selectObject ( "// User" );
            String user = userElt.getText();
            System.out.println(user);
            // Get userElt node element name attribute of an object object 
            the Attribute portAttr = userElt.attribute ( "name" );
             // get the value of the name attribute object node portAttr process. 1 
            String = Port portAttr.getStringValue ();
            System.out.println(port);
            // Get portAttr node name attribute value of an object, the method 2 
            String portValue = userElt.attributeValue ( "name" );
            System.out.println(portValue);
            
            
            Element passwordElt = (Element) document.selectSingleNode("//password");
            String password = passwordElt.getTextTrim();
            System.out.println(password);
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Output

 

Guess you like

Origin www.cnblogs.com/zhaideang/p/12310445.html