dom4j 通过 org.dom4j.DocumentFactory 设置命名空间来支持 带namespace 的 xpath
测试文件 test.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.zno</groupId> <artifactId>z-test</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>xmlunit</groupId> <artifactId>xmlunit</artifactId> <version>1.6</version> <scope>test</scope> </dependency> </dependencies> </project>
所需依赖
<dependencies> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.6</version> </dependency> </dependencies>
测试类
import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import javax.xml.transform.TransformerFactoryConfigurationError; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentFactory; import org.dom4j.Node; import org.dom4j.io.SAXReader; public class Dom4jDemo { public static void main(String[] args) throws TransformerFactoryConfigurationError, Exception { InputStream is = Dom4jDemo.class.getResourceAsStream("/test.xml"); SAXReader saxReader = new SAXReader(); DocumentFactory documentFactory = saxReader.getDocumentFactory(); Map<String, String> namespaceURIs = new HashMap<String, String>(); namespaceURIs.put("a", "http://maven.apache.org/POM/4.0.0"); namespaceURIs.put("xsi", "http://www.w3.org/2001/XMLSchema-instance"); documentFactory.setXPathNamespaceURIs(namespaceURIs); Document doc = saxReader.read(is); Node versionNode = doc.selectSingleNode("//a:project/a:dependencies/a:dependency[a:groupId/text()='junit' and a:artifactId/text()='junit']/a:version"); System.out.println(versionNode.getText()); } }
测试结果
4.12
注意:
1) 依赖 jaxen 而非 xalan (这个是转换时用的)
2)a:project 即是 qualified name ,全称:
http://maven.apache.org/POM/4.0.0/:project
3) xpath 语法:
4) 简单举例
String bean = doc.selectSingleNode("//generatorConfiguration/context/table[contains(@tableName,'"+name+"')]/@domainObjectName").getText(); String javaModel = doc.selectSingleNode("//generatorConfiguration/context/javaModelGenerator/@targetPackage").getText(); String sqlMap = doc.selectSingleNode("//generatorConfiguration/context/sqlMapGenerator/@targetPackage").getText(); String javaClient = doc.selectSingleNode("//generatorConfiguration/context/javaClientGenerator/@targetPackage").getText(); System.out.println(javaModel + "." + bean + ".java"); System.out.println(sqlMap + "." + bean + "Mapper.xml"); System.out.println(javaClient + "." + bean + "Mapper.java");
2021-03-16 补充:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="MBG" defaultModelType="conditional" targetRuntime="MyBatis3"> <connectionFactory> <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/> <property name="connectionURL" value="jdbc:mysql://192.168.200.208:3306/xx"/> <property name="userId" value="bbb"/> <property name="password" value="ddddd"/> <property name="useInformationSchema" value="true"/> </connectionFactory> <!-- tables --> <table schema="xx" tableName="s01_subsystem" domainObjectName="S01Subsystem"> <generatedKey column="s01_id" identity="true" sqlStatement="MySql" /></table> <table schema="xx" tableName="s02_page" domainObjectName="S02Page"> <generatedKey column="s02_id" identity="true" sqlStatement="MySql" /></table> </context> </generatorConfiguration>
取 table名列表:
List tables = doc.selectNodes("//generatorConfiguration/context/table/@tableName");
取连接信息(对property元素进行过滤,取name属性是某值的元素的value属性值):
String driver = doc.selectSingleNode("//generatorConfiguration/context/connectionFactory/property[@name='driverClass']/@value").getText(); String url = doc.selectSingleNode("//generatorConfiguration/context/connectionFactory/property[@name='connectionURL']/@value").getText(); String username = doc.selectSingleNode("//generatorConfiguration/context/connectionFactory/property[@name='userId']/@value").getText(); String password = doc.selectSingleNode("//generatorConfiguration/context/connectionFactory/property[@name='password']/@value").getText();