我使用正则表达式删除了XML中不需要的一些项目。现在,我需要打印所有包含多个子元素的 includedService 节点。
这是我使用的代码;
malwareLog = open(datetime.datetime.now().strftime("%Y%m%d - %H.%M " + pcName + " Malware scan log.txt"), "w+")
如果我运行此命令,则会得到以下答复。
malwareLog.open(malwareLog, "a+")
但是,我可以在includedService节点之外打印选定的元素。请帮忙!
* string test = read('classpath:PP1/data/Test.xml')
* string test= test.replaceAll("xsi:nil[^/]*", "")
* def Complete_XML = test
* def included = $Complete_XML/Envelope/Body/getPricePlanResponse/pricePlanSummary/includedService
* print included
答案 0 :(得分:1)
请记住,XML必须始终包装在某个元素或另一个元素内。试试这个:
* def included = $xml/Envelope/Body/getPricePlanResponse/pricePlanSummary//includedService
* def root = <root></root>
* def fun = function(x, i){ karate.set('root', '/includedService[' + (i + 1) + ']', x) }
* eval karate.forEach(included, fun)
* print root
您应该得到:
<root>
<ns4:includedService>
<ns4:term>0</ns4:term>
<ns4:brandId>1</ns4:brandId>
<ns4:feature>
<ns5:code>MBAPN</ns5:code>
<ns4:type/>
<ns4:additionalNumberRequiredInd>false</ns4:additionalNumberRequiredInd>
</ns4:feature>
<ns4:recurringCharge>0.0</ns4:recurringCharge>
<ns4:callingCircleFeaturesInd>false</ns4:callingCircleFeaturesInd>
</ns4:includedService>
<ns4:includedService>
<ns4:term>0</ns4:term>
<ns4:brandId>1</ns4:brandId>
<ns4:feature>
<ns5:code>MBAPN</ns5:code>
<ns4:type/>
<ns4:additionalNumberRequiredInd>false</ns4:additionalNumberRequiredInd>
</ns4:feature>
<ns4:recurringCharge>0.0</ns4:recurringCharge>
<ns4:callingCircleFeaturesInd>false</ns4:callingCircleFeaturesInd>
</ns4:includedService>
</root>
有关其他想法,请参考以下答案:https://stackoverflow.com/a/53553979/143475