2017-01-31 5 views
0

xmlファイルを解析して属性を出力しようとしていますが、機能しません。 webSimplified.xmlGroovy:XMLから属性を出力するのに問題があります

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    version="3.1"> 
    <servlet> 
     <servlet-name>jsp</servlet-name> 
     <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> 
     <init-param> 
      <param-name>fork</param-name> 
      <param-value>false</param-value> 
     </init-param> 
     <init-param> 
      <param-name>xpoweredBy</param-name> 
      <param-value>false</param-value> 
     </init-param> 
     <load-on-startup>3</load-on-startup> 
    </servlet> 
    <session-config> 
     <session-timeout>30</session-timeout> 
    </session-config> 
</web-app> 

ここ

def Server1 = new XmlParser().parse('c:\\temp\\webSimplified.xml') 
Server1.each { 
    println "Stuff in session-config: ${it}" 
    it.attributes().each { 
     println 'PLEASE PRINT OUT ATTRIBUTES: ' 
     println it.toString() 
    } 
} 

されており、ここに私のGroovyスクリプトを実行した結果である:ここで

はGroovyのコードで何が「それから、プリントアウトしていません.attributes()。each {'セグメント。それをどのように働かせることができますか?

Stuff in session-config: {http://xmlns.jcp.org/xml/ns/javaee}servlet[attributes={}; value=[{http://xmlns.jcp.org/xml/ns/javaee}servlet-name[attributes={}; value=[jsp]], {http://xmlns.jcp.org/xml/ns/javaee}servlet-class[attributes={}; value=[org.apache.jasper.servlet.JspServlet]], {http://xmlns.jcp.org/xml/ns/javaee}init-param[attributes={}; value=[{http://xmlns.jcp.org/xml/ns/javaee}param-name[attributes={}; value=[fork]], {http://xmlns.jcp.org/xml/ns/javaee}param-value[attributes={}; value=[false]]]], {http://xmlns.jcp.org/xml/ns/javaee}init-param[attributes={}; value=[{http://xmlns.jcp.org/xml/ns/javaee}param-name[attributes={}; value=[xpoweredBy]], {http://xmlns.jcp.org/xml/ns/javaee}param-value[attributes={}; value=[false]]]], {http://xmlns.jcp.org/xml/ns/javaee}load-on-startup[attributes={}; value=[3]]]] 
Stuff in session-config: {http://xmlns.jcp.org/xml/ns/javaee}session-config[attributes={}; value=[{http://xmlns.jcp.org/xml/ns/javaee}session-timeout[attributes={}; value=[30]]]] 

Process finished with exit code 0 
+0

あなたが印刷しようとしている属性? ''のものか、XMLをスキャンしてすべてのノードのすべての属性を表示しますか? – dmahapatro

+0

「session-config」要素に出力する属性がありません。あなたは "セッションタイムアウト"を取得しようとしていますか? –

答えて

0

もちろん何も印刷されません。私は他の答えで、属性を印刷する方法を教えましたが、属性はありませんが、子タグはあります。

これを試してみてください:

def Server1 = new XmlParser().parse(/c:\temp\webSimplified.xml/) 
Server1.each { 
    println "Stuff in ${it.name().localPart}: $it" 
    println() 
    it.children().each { 
     println 'PLEASE PRINT OUT child element: ' 
     println it 
     println() 
    } 
    3.times { println() } 
} 
0

これは動作します:

def Server1 = new XmlParser().parseText(''' 

<web-app 
    version="3.1"> 
    <servlet> 
     <servlet-name>jsp</servlet-name> 
     <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> 
     <init-param> 
      <param-name>fork</param-name> 
      <param-value>false</param-value> 
     </init-param> 
     <init-param> 
      <param-name>xpoweredBy</param-name> 
      <param-value>false</param-value> 
     </init-param> 
     <load-on-startup>3</load-on-startup> 
    </servlet> 
    <session-config> 
     <session-timeout>30</session-timeout> 
    </session-config> 
</web-app> 

''') 

出力:

servlet-name : jsp 
servlet-class : org.apache.jasper.servlet.JspServlet 
init-param : 
init-param : 
load-on-startup : 3 
session-timeout : 30 
0

をあなたはsesssionの設定値(あなたの質問に基づいて資格推測)を印刷したいと仮定すると、次のように動作します:

def str = ''' 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    version="3.1"> 
    <servlet> 
     <servlet-name>jsp</servlet-name> 
     <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> 
     <init-param> 
      <param-name>fork</param-name> 
      <param-value>false</param-value> 
     </init-param> 
     <init-param> 
      <param-name>xpoweredBy</param-name> 
      <param-value>false</param-value> 
     </init-param> 
     <load-on-startup>3</load-on-startup> 
    </servlet> 
    <session-config> 
     <session-timeout>30</session-timeout> 
    </session-config> 
</web-app> 
''' 

def xml = new XmlParser().parseText(str) 
xml.'session-config'.'*'.each { node -> 
    println "${node.name().localPart} -> ${node.text()}" 
} 

ノートは、そのノードがありますインスタンスgroovy.util.Nodeとnode.name()は、名前空間認識オブジェクトであるQNameのインスタンスを返します。 「ローカル名」を取得するために、QNameで「localPart」を呼び出します。

gpath発現xml.'session-config'.'*'.each手段:ルートノードから

(すなわちXML変数ここ)、名称「セッション設定」を持つすべての子ノードを選択し、そのノードの下のすべての子ノードを反復。

上記プリント:

session-timeout -> 30 
関連する問題