Parse xml/json[xpath/jpath]
import groovy.util.XmlSlurper
import groovy.util.XmlParser
import com.eviware.soapui.support.GroovyUtils
import com.jayway.jsonpath.* def xmlStr = '<root><one a1="uno!"/><one a1="aaa"/><two>Some text!</two></root>'
def rootNode // use XmlSlurper to parse xml, return GPathResult instances
rootNode = new XmlSlurper().parseText(xmlStr)
assert rootNode.name() == "root"
assert rootNode.one[0].@a1 == "uno!"
assert rootNode.two == "Some text!" // use XmlParser to parse xml, return Node objects
rootNode = new XmlParser().parseText(xmlStr)
assert rootNode.name() == "root"
assert rootNode.one[0].@a1 == "uno!"
assert rootNode.one[0]["@a1"] == "uno!"
assert rootNode.one[0].attribute("a1") == "uno!"
assert rootNode.two.text() == "Some text!"
rootNode.children().each { assert it.name() in ['one','two'] } /*
* When to use XmlSlurper or XmlParser?
* If you want to transform an existing document to another then XmlSlurper will be the choice.
* If you want to update and read at the same time then XmlParser is the choice.
* If you just have to read a few nodes XmlSlurper should be your choice, since it will not have to create a complete structure in memory".
*/ // use GroovyUtils.getXmlHolder().getDomNodes() to parse xml with xpath
def groovyUtils = new GroovyUtils(context)
def xmlHolder = groovyUtils.getXmlHolder(xmlStr)
def nodesArray = xmlHolder.getDomNodes("//one")
def oneNodeAttrValue = nodesArray[1].getAttribute("a1")
log.info oneNodeAttrValue // use JsonPath.read() to parse json with jpath
def jsonStr = '{"store":{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99}],"bicycle":{"color":"red","price":19.95}},"expensive":10}'
def authors = JsonPath.read(jsonStr, '$.store.book[*].author')
log.info authors
Parse xml/json[xpath/jpath]的更多相关文章
- parse XML & JSON & js
parse XML & JSON & js how to parse xml data into json in js? https://stackoverflow.com/quest ...
- XML+JSON面试题都在这里
XML+JSON常见面试题 什么是JSON和XML 什么是JSON和XML JSON:JavaScript Object Notation [JavaScript 对象表示法]. XML:extens ...
- 模块简介:(random)(xml,json,pickle,shelve)(time,datetime)(os,sys)(shutil)(pyYamal,configparser)(hashlib)
Random模块: #!/usr/bin/env python #_*_encoding: utf-8_*_ import random print (random.random()) #0.6445 ...
- JSON.parse 和 JSON.stringify 详解
JSON格式,(简写JavaScript Object Notation),是一种用于数据交换的文本格式,书写简单. 基于JavaScript原生语法,能够序列化对象.数组.数值.字符串.布尔值和 n ...
- Xml,Json,Hessian,Protocol Buffers序列化对比
简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...
- JSON.parse() 和 JSON.stringify()
JSON.parse()和JSON.stringify() 1.parse 用于从一个字符串中解析出json 对象.例如 var str='{"name":"cpf& ...
- xml json protobuf
本文为原创,转载请注明:http://www.cnblogs.com/gistao/ Background xml,json,protobuf都是格式化手段,喜欢哪个,会用哪个,该用哪个,用哪个? 随 ...
- iOS开发笔记3:XML/JSON数据解析
这篇主要总结在iOS开发中XML/JSON数据解析过程用到的方法.XML数据解析主要使用SAX方式的NSXMLParser以及DOM方式的GDataXML,JSON数据解析主要使用NSJSONSeri ...
- Silverlight项目笔记7:xml/json数据解析、TreeView、引用类型与数据绑定错误、图片加载、虚拟目录设置、silverlight安全机制引发的问题、WebClient缓存问题
1.xml/json数据解析 (1)xml数据解析 使用WebClient获取数据,获取到的数据实例化为一个XDocument,使用XDocument的Descendants(XName)方法获得对应 ...
随机推荐
- vc6.0如何显示行号以及出现版本不兼容问题
有时编译时,提示某某行有错,但是要定位到某一行的话,如果在编辑页面能够将行号显示出来,查找也就更方便了,下面我来介绍一下让VC6.0显示行号的方法. 工具/原料 VC6.0.显示行号的插件 方 ...
- laravel 实现上传 excel
//导入电话号码 public function postTel(){ set_time_limit(0); ini_set('memory_limit','1024M'); $params = In ...
- Ubuntu16.04安装Samba
在新装的ubuntu16.04下安装samba. 安装好samba后备份下配置文件: zhuben@zb:~$ sudo apt-get install samba zhuben@zb:~$ cd / ...
- Spring中servletFileUpload完成上传文件以及文本的处理
JSP: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnco ...
- iOS TextField用法大全
//初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...
- ngui的tween的tweenFactor属性
ngui的tween的tweenFactor属性 这个属性是用来记录动画运行的位置的.可以通过设置它来达到动画运行到一半从新设置从新开始
- express+gulp构建项目(三)gulp任务
这次来看一看gulp是怎么工作的. tasks/paths.js paths.js文件里存放的是gulp任务中需要导入的文件的路径和导出的路径. /** * gulp.src 地址 * gulp.de ...
- cf#382div2
A. 题意:字符串长度n,每次可向左向右跳k个格子.要求不能在障碍物处停留('#'),可以在空地处停留(' . ').给出字符串,从G开始,问能不能到达T. 分析:直接从G处开始向两边搜,如果能到T则 ...
- [解决]小程序要求的 TLS 版本必须大于等于 1.2
今天微信小程序发现wx.request不好使了,调试报错: 小程序要求的 TLS 版本必须大于等于 1.2 查官方文档 解决方法 在 PowerShell中运行以下内容, 然后重启服务器 # Enab ...
- 开源代码Window下搭建rtmp流媒体服务器
合肥程序员群:49313181. 合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q Q:408365330 E-Mail:egojit@qq.com 综合:有这样需求,将摄像头 ...