【问题标题】:Groovy Script .xml parsingGroovy 脚本 .xml 解析
【发布时间】:2017-06-10 19:15:39
【问题描述】:

请帮助: : 我如何在 .each 或 For Each 或其他什么中处理这些?我有一个 .xml 我正在尝试使用 Groovy 脚本进行解析。这是.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>

这是我的 Groovy 脚本:

def Server1 = new XmlParser().parse('c:\\temp\\server.xml')
Server1.Service.Connector.each {
    println "Stuff in Connector: ${it}"
}

结果如下:

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 块“连接器”的每个实例? 谢谢。

【问题讨论】:

    标签: groovy


    【解决方案1】:

    喜欢这样吗?

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

    【讨论】:

      【解决方案2】:

      我将属性名称,值提取添加到上一个答案。以下:

      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"
        }
      }
      

      打印:

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

      如果您不需要上述答案中的索引。

      【讨论】:

        猜你喜欢
        • 2021-12-31
        • 2013-02-21
        • 2023-04-05
        • 1970-01-01
        • 2016-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        相关资源
        最近更新 更多