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)方法获得对应 ...
随机推荐
- cocos2dx解决苹果正版ipv6的问题
苹果官方出了新的规定,要求新上架的app都必须单独支持ipv6-only的网络. 具体的要求链接:https://developer.apple.com/library/mac/documentati ...
- (转)pymysql 连接mysql数据库---不支持中文解决
往数据库里插入中文时出现异常:UnicodeEncodeError: 'latin-1' codec can't encode characters 就是编码的问题,pymysql默认的编码是lati ...
- asp 使用TreeView控件
这段代码为了使用 TreeNodeCheckChanged 事件,会有回刷新的效果: 不喜欢的可查看改进版,利用js控制选择操作,无界面刷新, “http://www.cnblogs.com/GoCi ...
- HashSet和HashMap的区别
HashSet和HashMap的区别.Java的HashSet类是由哈希表支持.它不保证 set 的迭代顺序:特别是它不保证该顺序恒久不变.此类允许使用 null 元素.HashSet类为基本操作提供 ...
- nginx 域名绑定端
server { listen 80; server_name taqing.me www.taqing.me; ##,绑定域名 location / { proxy_pass http://127. ...
- python unicode转中文及转换默认编码
一. 在爬虫抓取网页信息时常需要将类似"\u4eba\u751f\u82e6\u77ed\uff0cpy\u662f\u5cb8"转换为中文,实际上这是unicode的中文编码.可 ...
- C#中 Request, Request.params , Request.querystring , Request.Form 区别 与联系用法
C#中 Request, Request.params , Request.querystring , Request.Form 区别 与联系用法? Request.params , Request ...
- jquery实现可拖拽的div
由于项目中并未引入前端开发框架easyui.ext.没有现成的控件可以使用,今天时间算是充裕的时候,自己写了一个可以拖拽.放大缩小的例子.欢迎大家指正. 不啰嗦,上代码: 依赖的文件:jquery.j ...
- Java 中的 request 和response 区别
1.response 属于重定向请求: 其地址栏的URL会改变: 会向服务器发送两次请求: 2. request 属于请求转发: 其地址栏的URL不会改变: 向服务器发送一次请求: 举一个区分它们的简 ...
- 判断iframe是否加载完成的完美方法
var iframe = document.createElement("iframe"); iframe.src = "http://www.jb51.net" ...