在Groovy中读取一个Xml文件 [英] Read a Xml file in Groovy

查看:114
本文介绍了在Groovy中读取一个Xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我的功能

$ b

我正在阅读一个xml文件,它的格式如下所示,但不知道发生了什么问题。
$ b

  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)[\\\\\\\\\\\\\\\\\\\')) b

我的输出

  [pd:admin,u:,tns :, dh :, dn :, dp :, un:admin,x:c1164035531] 

My Xml

 <?xml version =1.0encoding =UTF-8 >?; 
< domainservice:GatewayNodeConfig imx:id =U:LtWHxY0ZEeGb2FwhP-xoGwadminconsolePort =15533adminconsoleShutdownPort =38207domainName =D_1035531nodeName =N_1035531dbConnectivity =ID_1>
< address imx:id =ID_2xsi:type =common:NodeAddresshost =absdiehttpPort =1531port =1532/>
< portals>
< NodeRef imx:id =ID_3xsi:type =common:NodeRefaddress =ID_2nodeName =N_1035531/>
< / portals>
< / domainservice:GatewayNodeConfig>
< domainservice:DBConnectivity imx:id =ID_1dbEncryptedPassword =ZmTXZDoYq0TyrU7fSaS9BrAlIuZyS2rw%2FafW1TLWE4g%3DdbHost =fortunerdbName =ORCLdbPort =1521dbType =ORACLEdbUsername =zx164935538​​8 />
< / imx:IMX>

注意:pd& x已经在地图中了

解决方案

XmlSlurper正在为您处理命名空间......只是摆脱命名空间部分节点名称(注意更多的Groovy映射管理)

pre $ 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()


I am reading a xml file in groovy something like below, but don't know what is going wrong.

Below is my function

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

}

My Output

[pd:admin, u:, tns:, dh:, dn:, dp:, un:admin, x:c1164035531]

My 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>

Note: pd & x are already there in the map

解决方案

XmlSlurper is handling the namespaces for you... Just get rid of the namespace part of the node names (note the more Groovy map management as well)

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()

这篇关于在Groovy中读取一个Xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆