测试文件 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>

test.xml

所需依赖

	<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 语法:

https://www.w3.org/TR/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");

dom4j 通过 org.dom4j.DocumentFactory 设置命名空间来支持 带namespace 的 xpath的更多相关文章

  1. dom4j 通过 org.dom4j.XPath 设置命名空间来支持 带namespace 的 xpath

    测试文件 test.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...

  2. phpsotrm 设置命名空间

    PHPStorm 添加支持 PSR-4 命名空间前缀设置 许久没有更新博客啦, 太忙了, 七月这最后一天来写点自己在使用 PHPStorm 上的小却很有用的功能吧. PHPStorm 默认是使用 PS ...

  3. Microsoft.VisualC 命名空间包含支持用 c + + 语言的代码生成和编译的类。 混合编程中使用COM接口指针

    Microsoft.VisualC 命名空间包含支持用 c + + 语言的代码生成和编译的类. Microsoft.VisualC.StlClr Unmanaged Code 和 Managed Co ...

  4. 【学习笔记】关于DOM4J:使用DOM4J解析XML文档

    一.概述 DOM4J是一个易用的.开源的库,用于XML.XPath和XSLT中.采用了Java集合框架并完全支持DOM.SAX.和JAXP. DOM4J最大的特色是使用大量的接口,主要接口都在org. ...

  5. yaf设置命名空间

    修改yaf配置文件 文件是:yaf.ini extension=yaf.so yaf.use_namespace=1 index文件. 目录是application/controllers/Index ...

  6. MVC5为WebAPI添加命名空间的支持

    前言 默认情况下,微软提供的MVC框架模板中,WebAPI路由是不支持Namespace参数的.这导致一些比较大型的项目,无法把WebApi分离到单独的类库中. 本文将提供解决该问题的方案. 微软官方 ...

  7. asp.net MVC5为WebAPI添加命名空间的支持

    前言 默认情况下,微软提供的MVC框架模板中,WebAPI路由是不支持Namespace参数的.这导致一些比较大型的项目,无法把WebApi分离到单独的类库中. 本文将提供解决该问题的方案. 微软官方 ...

  8. MVC5为WebAPI添加命名空间的支持1

    前言 默认情况下,微软提供的MVC框架模板中,WebAPI路由是不支持Namespace参数的.这导致一些比较大型的项目,无法把WebApi分离到单独的类库中. 本文将提供解决该问题的方案. 微软官方 ...

  9. discuz首页设置默认地址不带forum.php后缀的方法

    最近在研究discuz,上传安装几部搞定,打开首页跳到含有"/forum.php"的网址,到管理中心改了好一会儿也没好.那么如何实现discuz首页设置不带forum.php后缀呢 ...

随机推荐

  1. how2j网站前端项目——天猫前端(第一次)学习笔记1

    首先是公共页面的学习,有页头.页脚和搜索框. 一.页头就是天猫网站的置顶导航栏: 看似简单,实际做起来也不容易. 写html还是比较简单的,撸起袖子就可以写完.可要想做到上图的样式就难了,难就难在CS ...

  2. TabLayout+ViewPager的简单使用

    1.   build.gradle文件中加入 compile 'com.android.support:design:22.2.0' 2.写Xml文件,注意TabLayout的三个属性 app:tab ...

  3. npoi设置数据有效性

    npoi设置数据有效性 public void SetDataValidate(ISheet sheet, int firstCol, int lastCol) { CellRangeAddressL ...

  4. oracle 分页 where 三层

    查询[start,start+limit],包含start,包含start+limit,如start=21,limit=10结果就是21到30,包含21和30SELECT * FROM (SELECT ...

  5. auth 认证模块

    . auth认证模块: http://www.cnblogs.com/liwenzhou/p/9030211.html auth模块的知识点: . 创建超级用户 python manage.py cr ...

  6. c# 多个事件公用一个相应方法判断事件来源

    假设下边的相应方法有多个事件共同使用.根据事件的sender 判断来源,做相应的处理 假设事件来源DataManSystem;private void OnSystemConnected(object ...

  7. Oracle_高级功能(3) synonym和database link

    一.同义词synonymconnect sys/123 as sysdba;select * from emp;ORA-00942: 表或视图不存在create synonym emp for sco ...

  8. L1-027 出租(20)(STL-map代码)

    L1-027 出租(20 分) 下面是新浪微博上曾经很火的一张图: 一时间网上一片求救声,急问这个怎么破.其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2 ...

  9. java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;

    在Tomcat下部署应用时会报这个错误,参考以下这篇博客:http://blog.csdn.net/robinsonmhj/article/details/37653189,删除Tomcat目录下we ...

  10. VS Installer教程

    本文主要讲解利用VS2010下的Visual Studio Installer打包Zigbee程序(VS2010编写)的过程. 1.打开Zigbee程序,在解决方案中添加“新建项目”-->其他项 ...