简体   繁体   English

用groovy文件中的字符串替换XML路径

[英]Replace XML path with a string in groovy file

I'm trying to replace a xml rootnode with a string, but it doesn't allow me. 我正在尝试用字符串替换xml rootnode,但是它不允许我这样做。

I was trying to give it as String str = "SOAP-ENV:Body'.'ns1:creditCardResponse"; 我试图把它作为String str = "SOAP-ENV:Body'.'ns1:creditCardResponse";

I should not repeat SOAP-ENV:Body'.'ns1:creditCardResponse in all these lines. 我不应该在所有这些行中重复SOAP-ENV:Body'.'ns1:creditCardResponse

 def rootnode = new XmlParser().parseText(responseXml);
 status = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Status.text();
 errorCode = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Errorcode.text();
 errorInfo = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Errorinfo.text();
 referenceCode = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.ReferenceCode.text();
 requestIp = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.RequestIP.text()

Any ideas would be greatly appreciated. 任何想法将不胜感激。

Thanks. 谢谢。

Remember that these "paths" are just a series of normal groovy property accesses, so you can store any intermediate point in the path as a variable and continue navigating from there: 请记住,这些“路径”只是一系列常规的groovy属性访问,因此您可以将路径中的任何中间点存储为变量,然后继续从那里导航:

deg rtn = rootnode.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return
status = rtn.Status.text()
errorCode = rtn.Errorcode.text()
// etc.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM