Groovy XMLParser - 更新节点文本

时间:2014-09-29 08:54:10

标签: xml groovy

我有一些XML文件,如果 name == testName ,我想更改 name 。所以我写了这样的东西,但它没有用。我在哪里做错了?它打印给我参数类型不匹配,我不知道为什么。

String xmlData = "
    <logger>
      <logLevel>
        <id>1</id>
        <idn>id1</idn>
        <name>testName</name>
        <level>warn</level>
        <logLevelsPos>
          <package id="97724539">
            <packName>package1</packName>
          </package>
          <package id="98264591">
            <packName>package2</packName>
          </package>
        </logLevelsPos>
      </logLevel>
      <logLevel>
        <id>2</id>
        <idn>id2</idn>
        <name>nameName</name>
        <level>error</level>
        <logLevelsPos>
          <package id="12732539">
            <packName>package2</packName>
          </package>
          <package id="43264591">
            <packName>package5</packName>
          </package>
        </logLevelsPos>
      </logLevel>
    </logger>";

def loggr = new groovy.util.XmlParser().parseText( xmlData );
for (def it : loggr.children()){
    if(it.name.text().equals("testName") ) {
        it.name.value = "ds";
        //argument type mismatch ...
    }

1 个答案:

答案 0 :(得分:7)

您必须访问name中文本节点的值。所以它是it.name[0].value = 'ds'