我在groovy中读取xml文件,如下所示,但不知道出了什么问题。
以下是我的功能
def setEnvironment(Map env,Map params)
{
def records=new XmlSlurper().parse(env.get("ABC_HOME")+"/isp/config/nodemeta.xml")
def jdbcurl=records.'domainservice:DBConnectivity'[0].@dbConnectString
params.put("dn", records.'domainservice:GatewayNodeConfig'[0].@domainName)
params.put("dh", records.'domainservice:GatewayNodeConfig'[0].'address'[0].@host)
params.put("dp", records.'domainservice:GatewayNodeConfig'[0].'address'[0].@httpPort)
params.put("u", records.'domainservice:DBConnectivity'[0].@dbUsername)
if(jdbcurl==null||jdbcurl.size()==0)
{
params.put("tns", records.'domainservice:DBConnectivity'[0].@dbHost)
}
else
{
params.put("tns", jdbcurl.find("(?<=%2F%2F)[\\d\\w_]+"))
}
println params
}
我的输出
[pd:admin, u:, tns:, dh:, dn:, dp:, un:admin, x:c1164035531]
我的Xml
<?xml version="1.0" encoding="UTF-8"?>
<imx:IMX xmlns:imx="http://com.abc.imx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" serializationSpecVersion="4.0" crcEnabled="0" xmlns:domainservice="http://com.abc.isp.metadata.domainservice/2" versiondomainservice="2.4.1" xmlns:common="http://com.abc.isp.metadata.common/2" versioncommon="2.2.0" xsi:schemaLocation="http://com.abc.imx IMX.xsd http://com.abc.isp.metadata.domainservice/2 com.abc.isp.metadata.domainservice.xsd http://com.abc.isp.metadata.common/2 com.abc.isp.metadata.common.xsd">
<domainservice:GatewayNodeConfig imx:id="U:LtWHxY0ZEeGb2FwhP-xoGw" adminconsolePort="15533" adminconsoleShutdownPort="38207" domainName="D_1035531" nodeName="N_1035531" dbConnectivity="ID_1">
<address imx:id="ID_2" xsi:type="common:NodeAddress" host="absdie" httpPort="1531" port="1532"/>
<portals>
<NodeRef imx:id="ID_3" xsi:type="common:NodeRef" address="ID_2" nodeName="N_1035531"/>
</portals>
</domainservice:GatewayNodeConfig>
<domainservice:DBConnectivity imx:id="ID_1" dbEncryptedPassword="ZmTXZDoYq0TyrU7fSaS9BrAlIuZyS2rw%2FafW1TLWE4g%3D" dbHost="fortuner" dbName="ORCL" dbPort="1521" dbType="ORACLE" dbUsername="zx1649355388"/>
</imx:IMX>
注意:pd&amp; x已经在地图中
答案 0 :(得分:1)
XmlSlurper正在为你处理名称空间......只需删除节点名称的命名空间部分(注意更多的Groovy地图管理)
params.dn = records.GatewayNodeConfig[0].@domainName.text()
params.dh = records.GatewayNodeConfig[0].address[0].@host.text()
params.dp = records.GatewayNodeConfig[0].address[0].@httpPort.text()
params.u = records.DBConnectivity[0].@dbUsername.text()