2017-01-25 14 views
0

助けてください::どのようにこれらのそれぞれを、それぞれの、あるいはFor Eachなどで扱うことができますか?私は.xmlを持っています。私はGroovyスクリプトを解析しようとしています。ここでの.xmlは次のとおりです。ここでGroovy Script .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ブロック 'Connector'のすべてのインスタンスに対して、 'port'や 'protocol'などの項目を個別にrintしますか?おかげさまで

答えて

0

このような?

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

属性名、値抽出を前の回答に追加します。次

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

あなたは上記の回答でインデックスを必要としない場合。