Groovy:麻烦从xml打印出属性

时间:2017-01-31 16:03:08

标签: groovy

我正在尝试解析xml文件并打印出属性,但它无法正常工作。

以下是Groovy代码:

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()
    }
}

这是webSimplified.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>

以下是运行我的Groovy脚本的结果:没有任何内容从&#39; it.attributes()打印出来。每个{&#39;分割。我怎样才能让它发挥作用?

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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() }
}

答案 2 :(得分: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()}"
}

请注意,node是groovy.util.Node的实例,node.name()返回QName的实例,该实例是名称空间感知对象。获取本地名称&#39;我们称之为“localPart&#39;在QName上。

gpath表达式xml.'session-config'.'*'.each表示:

从根节点(即此处的xml变量),选择名称为&#39; session-config&#39;的所有子节点。然后迭代该节点下的所有子节点。

以上版画:

session-timeout -> 30