0

我有一个这样的xml:

<MMP>
<MERCHANT>
<RESPONSE>
<url>http://203.114.240.77/paynetz/epi/fts</url>
<param name="ttype"></param>
<param name="tempTxnId"></param>
</RESPONSE>
</MERCHANT>
</MMP>

我怎样才能得到 和 的ttypetempTxnId。我试过:

def details = new XmlParser().parseText(response)
details.MMP.RESPONSE //which returns the whole xml itself rather than its contents

我在哪里犯了错误?

提前致谢。

4

1 回答 1

5

给定:

def response = '''<MMP>
                 |  <MERCHANT>
                 |    <RESPONSE>
                 |      <url>http://203.114.240.77/paynetz/epi/fts</url>
                 |      <param name="ttype">a</param>
                 |      <param name="tempTxnId">b</param>
                 |    </RESPONSE>
                 |  </MERCHANT>
                 |</MMP>'''.stripMargin()

然后:

def (ttype,tempTxn) = new XmlParser().parseText( response )
                                     .MERCHANT.RESPONSE.param.with { r ->
  [ r.find { it.@name == 'ttype' }?.text(),
    r.find { it.@name == 'tempTxnId' }?.text() ]
}

assert ttype == 'a'
assert tempTxn == 'b'
于 2013-03-18T11:53:06.960 回答