Java 正则表达式匹配xml节点
xml内容
xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.test.dao.sysResourcesMapper" >
<resultMap id="BaseResultMap" type="com.zh.car.entity.SysResources" >
<id column="id" property="id"/>
<result column="alias" property="alias" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="parent_id" property="parentId" jdbcType="VARCHAR" />
<result column="url" property="url" jdbcType="VARCHAR" />
<result column="sort" property="sort" jdbcType="INTEGER" />
<result column="icon_cls" property="iconCls" jdbcType="VARCHAR" />
<result column="rules" property="rules" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
id,alias,name,parent_id,url,sort,icon_cls,rules,create_time
</sql>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.test.dao.sysResourcesMapper" >
<resultMap id="BaseResultMap" type="com.zh.car.entity.SysResources" >
<id column="id" property="id"/>
<result column="alias" property="alias" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="parent_id" property="parentId" jdbcType="VARCHAR" />
<result column="url" property="url" jdbcType="VARCHAR" />
<result column="sort" property="sort" jdbcType="INTEGER" />
<result column="icon_cls" property="iconCls" jdbcType="VARCHAR" />
<result column="rules" property="rules" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
id,alias,name,parent_id,url,sort,icon_cls,rules,create_time
</sql>
</mapper>
正则表达式
java
Pattern pattern = Pattern.compile("<resultMap\\s+id=\"BaseResultMap\"[\s\S]+?>([\s\S]+?)</resultMap>");
Matcher m = pattern.matcher(xml);
if (m.find()) {
System.out.println(m.group());
}
Pattern pattern = Pattern.compile("<resultMap\\s+id=\"BaseResultMap\"[\s\S]+?>([\s\S]+?)</resultMap>");
Matcher m = pattern.matcher(xml);
if (m.find()) {
System.out.println(m.group());
}
输出
xml
<resultMap id="BaseResultMap" type="com.test.entity.SysResources" >
<id column="id" property="id"/>
<result column="alias" property="alias" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="parent_id" property="parentId" jdbcType="VARCHAR" />
<result column="url" property="url" jdbcType="VARCHAR" />
<result column="sort" property="sort" jdbcType="INTEGER" />
<result column="icon_cls" property="iconCls" jdbcType="VARCHAR" />
<result column="rules" property="rules" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>
<resultMap id="BaseResultMap" type="com.test.entity.SysResources" >
<id column="id" property="id"/>
<result column="alias" property="alias" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="parent_id" property="parentId" jdbcType="VARCHAR" />
<result column="url" property="url" jdbcType="VARCHAR" />
<result column="sort" property="sort" jdbcType="INTEGER" />
<result column="icon_cls" property="iconCls" jdbcType="VARCHAR" />
<result column="rules" property="rules" jdbcType="VARCHAR" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>
如果是要获取resultMap里面的节点:
java
Pattern pattern = Pattern.compile("<resultMap\\s+id=\"BaseResultMap\"[\s\S]+?>([\s\S]+?)</resultMap>");
Matcher m = pattern.matcher(xml);
if (m.find()) {
System.out.println(m.group(1));
}
Pattern pattern = Pattern.compile("<resultMap\\s+id=\"BaseResultMap\"[\s\S]+?>([\s\S]+?)</resultMap>");
Matcher m = pattern.matcher(xml);
if (m.find()) {
System.out.println(m.group(1));
}