Java xmlPath无法从xml获取要列出的特定对象

Java xmlPath无法从xml获取要列出的特定对象,java,xml,rest-assured,Java,Xml,Rest Assured,我最近开始使用rest-assured测试一些xml。以下是一个例子: <activity> <shopping> <category type="groceries" label="chocolate" /> <category type="groceries" label="chocolate" /> <category type="present" label="chocolate

我最近开始使用rest-assured测试一些xml。以下是一个例子:

<activity>
    <shopping>
        <category type="groceries" label="chocolate" />
        <category type="groceries" label="chocolate" />
        <category type="present" label="chocolate" />
        <container number="eight" color="green" />
    </shopping>
    <shopping>
        <category type="groceries" label="chocolate" />
        <category type="groceries" label="chocolate" />
        <category type="present" label="chocolate" />
        <container number="eight" color="green" />
    </shopping>
    <shopping>
        <category type="groceries" label="chocolate" />
        <category type="groceries" label="chocolate" />
        <category type="present" label="chocolate" />
        <container number="eight" color="green" />
    </shopping>
</activity>
这非常好,当我调试它时,我得到了我想要实现的东西:


        List <String> lista = xmlPath.getList("activity.shopping.list().container.findAll{it.@number == 'eight'}.@color", String.class);


List lista=xmlPath.getList(“activity.shopping.List().container.findAll{it.@number='eight'}.@color”,String.class);
这是一个三元素列表,其中每个元素都等于“绿色”

但当我尝试检查是否每次购物都有一个类别type=“present”,然后检查这些类别是否都有label=“chocolate”,我收到一个空列表:


        List <String> lista2 = xmlPath.getList("activity.shopping.list().category.findAll{it.@type == 'present'}.@label", String.class);


List lista2=xmlPath.getList(“activity.shopping.List().category.findAll{it@type=='present'}.@label',String.class”);

你知道我遗漏了什么或者解决方案应该是什么样子吗?非常感谢您的帮助。

这只是猜测,但您可能需要在
.category
之后添加
.list()


另外,您可能想调用变量
容器

我设法找到了解决方案。首先,我想说明为什么你的建议没有让我满意。这是真的,例如它可以工作,但是如果xml看起来像这样,它就不能工作

<activity>
    <shopping>
        <category type="groceries" label="chocolate" />
        <category type="groceries" label="chocolate" />
        <container number="eight" color="green" />
    </shopping>
    <shopping>
        <category type="groceries" label="chocolate" />
        <category type="groceries" label="chocolate" />
        <category type="present" label="chocolate" />
        <category type="present" label="chocolate" />
        <container number="eight" color="green" />
    </shopping>
    <shopping>
        <category type="groceries" label="chocolate" />
        <category type="groceries" label="chocolate" />
        <category type="present" label="chocolate" />
        <container number="eight" color="green" />
    </shopping>
</activity>
在这种情况下,测试仍然通过,即使不是在我们的每一次购物中

我设法弄明白的是,将购物映射到一个列表上,然后在循环中检查类别:

    @Test
    public void test4(){
        List<Node> shoppings = new XmlPath(TestXML2).getList("activity.shopping.list()");
        for (Node categories : shoppings) {
            String val  = categories.getPath("category.findAll{it.@type == 'present'}.@label");
            assertThat(val, equalTo("chocolate"));
        }
    }
@测试
公共无效测试4(){
List shoppings=new-XmlPath(TestXML2.getList)(“activity.shopping.List()”);
用于(节点类别:购物){
字符串val=categories.getPath(“category.findAll{it@type=='present'}.@label”);
资产(val,equalTo(“巧克力”));
}
}

在上面的例子中,该测试将如预期的那样失败,因为它会正确地检查每次购物中是否有具有所需属性的所需类别。

为了清晰起见,我将container更改为container,谢谢。这是真的,category之后的
.list()
返回具有所需对象的列表,但没有所需的效果。它不会检查每个购物中是否有所需的类别。它会检查所有的商店是否都有想要的类别。例如,如果我将
从一次购物中移除,然后移动到另一次购物中,此解决方案的效果将是相同的。我正在寻找解决方案,这个例子的效果会有所不同。
    @Test
    public void test3() {
        final XmlPath xmlPath = new XmlPath(TestXML2);
        assertThat(xmlPath.getList("activity.shopping.category.findAll{it.@type == 'present'}.@label", String.class), everyItem(equalTo("chocolate")));
    }
    @Test
    public void test4(){
        List<Node> shoppings = new XmlPath(TestXML2).getList("activity.shopping.list()");
        for (Node categories : shoppings) {
            String val  = categories.getPath("category.findAll{it.@type == 'present'}.@label");
            assertThat(val, equalTo("chocolate"));
        }
    }