[英]Map from XML using rest-assured
I'm trying to set up an integration test using rest-assured. 我正在尝试使用可放心的设置集成测试。 In one of my test cases I have to validate some properties of an XML file with rest-assured's XmlPath which seems to use Groovy's GPath.
在我的一个测试用例中,我必须使用放心的XmlPath来验证XML文件的某些属性,该XmlPath似乎使用了Groovy的GPath。
I have an XML document with the following structure (the ids are unique): 我有一个具有以下结构的XML文档(这些ID是唯一的):
<rootelement>
<someelement id="1234" type="a">
<property key="hello" value="world" />
<property key="name" value="a name" />
<property key="status" value="new" />
<child target="645823" type="a" />
<child target="7482" type="b" />
<child target="8942" type="c">
<property key="pro" value="yes" />
</child>
</someelement>
<someelement>
...
</someelement>
<rootelement>
Ideally, given a someelement id, I want to get a map of it's properties, ie assuming the given someelement id is 1234 I'd like to get a map that looks like the following: {"hello": "world", "name": "a name", "status": "new"}
. 理想情况下,给定someelement id,我想获取其属性的映射,即假设给定的someelement id为1234,我想获取一个如下所示的映射:
{"hello": "world", "name": "a name", "status": "new"}
。 How would I do this? 我该怎么做? I know that there's a getMap method in XmlPath, but I couldn't figure out which expression I'd have to use.
我知道XmlPath中有一个getMap方法,但是我无法弄清楚我必须使用哪个表达式。
If it's not possible to get the properties as a map, I would be content with getting a list of the keys and a list for the values. 如果无法将属性作为地图获取,那么我会很满意获得键列表和值列表。 Again, I don't know which expression I have to use.
同样,我不知道必须使用哪个表达式。 I tried something like that:
我尝试过这样的事情:
xmlPath.getList("**.find {it.@id = '1234'}.property.@key", String.class)
However, it doesn't find anything. 但是,它找不到任何东西。
You can do this with Groovy (assuming xml
is a String containing your xml) 您可以使用Groovy做到这一点(假设
xml
是一个包含xml的字符串)
def map = new XmlParser().parseText( xml )
.someelement
.find { it.@id == '1234' }
.property
.collectEntries { [ it.@key, it.@value ] }
assert map == [ hello:'world', name:'a name', status:'new' ]
Never used rest-assured, so I can't be much help in that direction though :-( 从未使用过放心的服务,因此尽管如此,我在这个方向上无济于事:-(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.