代码之家  ›  专栏  ›  技术社区  ›  Rebse

Groovy xmlparser get属性值

  •  1
  • Rebse  · 技术社区  · 7 年前

    s='''<?xml version="1.0" encoding="UTF-8"?>
    <web-ext
      xmlns="http://websphere.ibm.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-ext_1_0.xsd"
      version="1.0">
      <reload-interval value="3"/>
      <context-root uri="foo/bar" />
      <enable-directory-browsing value="false"/>
      <enable-file-serving value="true"/>
      <enable-reloading value="true"/>
      <enable-serving-servlets-by-class-name value="false" />
    </web-ext>
    '''
    
    def contextroot
    def xml = new XmlParser(false,false).parseText(s)
    xml.each {
     if (it.name() == "context-root")
     contextroot = it.attributes().uri
    }
    

    它给了我正确的值。但是有更直接的方法吗?像这样的

    xml.name("context-root").uri
    

    不起作用。

    1 回复  |  直到 7 年前
        1
  •  5
  •   Szymon Stepniak    7 年前

    如果要直接访问此属性,可以使用

    xml.'context-root'[0].@uri