2017-01-25 109 views
0

請幫助::我該如何處理每個.each或For Each或其他?我有一個.xml我想用Groovy腳本解析。這裏是.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}" 
} 

爲了p對每一個單獨的項目,比如'端口'和'協議'等等,對於.xml塊'連接器'的每個實例? 謝謝。

回答

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

,如果你不需要在上面的回答索引。