在上一篇中提到,如果xml文档中有namespace的情况,如果没有手动设置namespace的话,是获取不到节点数据的。那么要怎么获取多个namesapce的xml文档中,指定的id的指定名称的属性。
具体情况如下 :
spring配置文件下有多个命名空间:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
">
<context:property-placeholder location="classpath:config/sys/*.properties"/>
<context:component-scan base-package="cn.expopay.projectTemp.projectTempServer"
name-generator="org.springframework.beans.factory.support.DefaultBeanNameGenerator">
</context:component-scan>
<bean id="exceptionInterceptor" class="cn.xxx.projectTemp.projectTempServer.interceptor.ExceptionInterceptor"/>
<aop:config>
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(public * cn.xx.projectTemp.projectTempServer.service..*.*(..)) "/>
<!--在该切入点使用自定义拦截器-->
<aop:advisor pointcut-ref="pointcut" advice-ref="exceptionInterceptor"/>
</aop:config>
</beans>
在以上的xml中,有多个命名空间。那么要怎么获取不同命名空间下的节点呢 ?
需求1:获取context命名空间下的 component-scan节点的 base-packag属性。
document = XmlUtils.getXmlDocment("/server_resources/config/spring/springContext.xml");
if(document == null){
return;
}
namespace = new HashMap();
// 获得命名空间
String nsURI = document.getRootElement().getNamespaceURI();
namespace.put("xmlns",nsURI);
namespace.put("context","http://www.springframework.org/schema/context");
XPath xpath_context_scan = document.createXPath("//xmlns:beans/context:component-scan/@base-package");
// 设置scan package
String baseScanPackage = "cn.xxx." + projectName + "." + projectName + "Server";
document = XmlUtils.setXmlAttribute(document,namespace,xpath_context_scan,baseScanPackage);
/**
* 设置xml属性
* @param document
* @param namespace
* @param xpath
* @param attributeText
* @return
* @throws Exception
*/
public static Document setXmlAttribute(Document document, Map namespace, XPath xpath, String attributeText) throws Exception{
if(document == null || namespace== null|| namespace.isEmpty()){
return null;
}
// 创建解析路径,就是在普通的解析路径前加上map里的key值
// 解决xpath在有命名空间情况下获取不到就节点问题
xpath.setNamespaceURIs(namespace);
Attribute attribute = (Attribute) xpath.selectSingleNode(document);
if(attribute != null){
// 修改属性
attribute.setText(attributeText);
}else{
throw new Exception(xpath + "节点为空!");
}
return document;
}
需求2:获取bean命名空间下的指定id的class属性
XPath xpath_exceptionn_interceptor = document.createXPath("//xmlns:beans/xmlns:bean[@id=\"exceptionInterceptor\"]/@class");
需求3:获取aop命名空间下pointcut的expression属性
XPath xpath_pointcut = document.createXPath("//xmlns:beans/aop:config/aop:pointcut[@id=\"control\"]/@expression");