Groovy xml解析

Groovy xml解析,groovy,Groovy,请帮助:我如何在一个。每个或为每个或任何东西中处理每一个?我正试图用Groovy脚本解析一个.xml文件。以下是.xml文件: <Server port="8005" shutdown="SHUTDOWN"> <Service name="Catalina"> <Connector port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1" connectionTimeout="20000" redirectP

请帮助:我如何在一个。每个或为每个或任何东西中处理每一个?我正试图用Groovy脚本解析一个.xml文件。以下是.xml文件:

<Server port="8005" shutdown="SHUTDOWN">
  <Service name="Catalina">
    <Connector port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
         maxThreads="150" scheme="https" secure="true"
         keystoreFile="/something/q2_reports/server_QA1/keystorea.jks" keystorePass="password"
         clientAuth="false" sslProtocol="TLS" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
  </Service>
</Server>
结果如下:

Stuff in Connector: Connector[attributes={port=8080, URIEncoding=UTF-8, protocol=HTTP/1.1, connectionTimeout=20000, redirectPort=8443}; value=[]]

Stuff in Connector: Connector[attributes={port=8443, protocol=HTTP/1.1, SSLEnabled=true, maxThreads=150, scheme=https, secure=true, keystoreFile=/cwtapp/e2_reports/jasper_server_QA1/keystore.jks, keystorePass=password, clientAuth=false, sslProtocol=TLS}; value=[]]

Stuff in Connector: Connector[attributes={port=8009, protocol=AJP/1.3, redirectPort=8443}; value=[]]
我的问题是:我可以在

Server1.Service.Connector.each {
    println "Stuff in Connector: ${it}"
}
为了对.xml块“Connector”的每个实例分别打印每个项目,例如“port”和“protocol”等等? 谢谢。

像这样吗

def Server1 = new XmlParser().parse('c:\\temp\\server.xml')
Server1.Service.Connector.each {
    println 'Attributes of Connector:'
    it.attributes().each { println it }
    println()
}

我将在前面的答案中添加属性名和值提取。以下是:

def str = '''\
<Server port="8005" shutdown="SHUTDOWN">
  <Service name="Catalina">
    <Connector port="8080" URIEncoding="UTF-8" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"/>
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
         maxThreads="150" scheme="https" secure="true"
         keystoreFile="/something/q2_reports/server_QA1/keystorea.jks" keystorePass="password"
         clientAuth="false" sslProtocol="TLS" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
  </Service>
</Server>'''

def xml = new XmlParser().parseText(str)
xml.Service.Connector.indexed(1).each { i, connectorNode -> 
  println "\nConnector $i"
  connectorNode.attributes().each { k, v -> 
    println "$k -> $v"
  }
}
你可以这样写:

xml.Service.Connector.each { connectorNode ->
如果您不需要上述答案中的索引

Connector 1
port -> 8080
URIEncoding -> UTF-8
protocol -> HTTP/1.1
connectionTimeout -> 20000
redirectPort -> 8443

Connector 2
port -> 8443
protocol -> HTTP/1.1
SSLEnabled -> true
maxThreads -> 150
scheme -> https
secure -> true
keystoreFile -> /something/q2_reports/server_QA1/keystorea.jks
keystorePass -> password
clientAuth -> false
sslProtocol -> TLS

Connector 3
port -> 8009
protocol -> AJP/1.3
redirectPort -> 8443
xml.Service.Connector.each { connectorNode ->