dom4j結合xpath獲取多名稱空間xml中指定id、指定屬性節點
阿新 • • 發佈:2019-02-12
在上一篇中提到,如果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");