zoukankan      html  css  js  c++  java
  • rest-assured的xmlPath使用方法总结

    xmlPath的使用方法跟JsonPath的使用方法相近,下面简单总结一下:

    准备xml文件数据:

     1 <records>
     2    <car name='HSV Maloo' make='Holden' year='2006'>
     3      <country>Australia</country>
     4      <record type='speed'>Pickup Truck with speed of 271kph</record>
     5    </car>
     6    <car name='P50' make='Peel' year='1962'>
     7      <country>Isle of Man</country>
     8      <record type='size'>Street-Legal Car at 99cm wide, 59kg</record>
     9    </car>
    10    <car name='Royale' make='Bugatti' year='1929'>
    11      <country>France</country>
    12      <record type='price'>Most Valuable Car at $15 million</record>
    13    </car>
    14 </records>

    下面我们使用rest-assured的xmlPath来简单的提取一些值:

     1 //recordsXml代表xml文件数据
     2 XmlPath xmlPath = new XmlPath(recordsXml);
     3 //设置根路径
     4 xmlPath.setRoot("records");
     5 
     6 //获取第一个car的name属性
     7 String nameOfFirstCar = xmlPath.get("car[0].@name");
     8 // 获取最后一个car的country属性
     9 String nameOfCountryForLastCar = xmlPath.get("car[-1].country");
    10 // 获取前两个记录的年份,作为一个list(使用ints)
    11 List<string> years = xmlPath.getList("car[0..1].@year", Integer.class);

    与JsonPath一样,xmlPath同样支持简单提取某个值:

    1 // "from"从XmlPath中静态导入
    2 String firstCarName = from(recordXml).get("records.car[0].@name");

    你也可以做一些复杂点的操作,比如以int的形式获取第一条记录的年份:

    1 // Year is 1929
    2 int year = from(recordXml).
    3     getInt("records.car.@year.list()*.toInteger().min()");

    我们首先将所有year属性转换为一个list,list中的所有元素都调用toInteger() 方法(这需要year属性在xml文档中以String类型展示),然后调用min()方法,返回list中的最小元素:1929

  • 相关阅读:
    一文看懂Fluentd语法
    mongo 使用聚合合并字段
    加速开发流程的 Dockerfile 最佳实践
    nodejs之RSA加密/签名
    nodejs之https双向认证
    自签证书生成
    白话理解https
    一文看懂k8s Deployment yaml
    基于xtermjs实现的web terminal
    intelliJ 中文设置
  • 原文地址:https://www.cnblogs.com/lwjnicole/p/8270277.html
Copyright © 2011-2022 走看看