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]的更多相关文章

  1. parse XML & JSON & js

    parse XML & JSON & js how to parse xml data into json in js? https://stackoverflow.com/quest ...

  2. XML+JSON面试题都在这里

    XML+JSON常见面试题 什么是JSON和XML 什么是JSON和XML JSON:JavaScript Object Notation [JavaScript 对象表示法]. XML:extens ...

  3. 模块简介:(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 ...

  4. JSON.parse 和 JSON.stringify 详解

    JSON格式,(简写JavaScript Object Notation),是一种用于数据交换的文本格式,书写简单. 基于JavaScript原生语法,能够序列化对象.数组.数值.字符串.布尔值和 n ...

  5. Xml,Json,Hessian,Protocol Buffers序列化对比

    简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...

  6. JSON.parse() 和 JSON.stringify()

    JSON.parse()和JSON.stringify()   1.parse 用于从一个字符串中解析出json 对象.例如 var str='{"name":"cpf& ...

  7. xml json protobuf

    本文为原创,转载请注明:http://www.cnblogs.com/gistao/ Background xml,json,protobuf都是格式化手段,喜欢哪个,会用哪个,该用哪个,用哪个? 随 ...

  8. iOS开发笔记3:XML/JSON数据解析

    这篇主要总结在iOS开发中XML/JSON数据解析过程用到的方法.XML数据解析主要使用SAX方式的NSXMLParser以及DOM方式的GDataXML,JSON数据解析主要使用NSJSONSeri ...

  9. Silverlight项目笔记7:xml/json数据解析、TreeView、引用类型与数据绑定错误、图片加载、虚拟目录设置、silverlight安全机制引发的问题、WebClient缓存问题

    1.xml/json数据解析 (1)xml数据解析 使用WebClient获取数据,获取到的数据实例化为一个XDocument,使用XDocument的Descendants(XName)方法获得对应 ...

随机推荐

  1. road习题(一)

    答案:[D] 答案:[C] 分析需要靠人 答案:[B] 答案:[B] c语言本身支持自定义函数 答案:[B] Virtual User Generator:是一个脚本开发组件 说白了就是虚拟机用户发生 ...

  2. M2事后分析报告

    设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 这次M2预想的就是解决3个主要问题,1:增加查询自己购买或者发布记录的功能,2:优化 所有的网络连接 ...

  3. (十二)select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET

    select函数用于在非阻塞中,当一个套接字或一组套接字有信号时通知你,系统提供select函数来实现多路复用输入/输出模型,原型:int select(int maxfd,fd_set *rdset ...

  4. Sprint1(第二天11.15)

    Sprint1(第二天11.15) Sprint1第一阶段 1.类名:软件工程-第一阶段 2.时间:11.14-11.23 3.选题内容:web版-餐厅到店点餐系统 4.团队博客地址: http:// ...

  5. HTML5中引入的关键特性

    新特性 描述 accesskey 定义通过键盘访问元素的快捷键 contenteditable 该特性设置为true时,浏览器应该允许用户编辑元素的内容.不指定变化后的内容如何保存 contextme ...

  6. PE文件学习系列笔记四-C++实现PE文件的分析

    合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q  Q:408365330     E-Mail:egojit@qq.com 综述: 首 ...

  7. 记一次log4j日志文件输出错误的解决

    log4j错误信息:log4j:ERROR Failed to rename [D:/logs/wmts_] to [D:/logs/wmts_2015-12-21.log ]. 起因:部门网站使用B ...

  8. hdu 5927 Auxiliary Set

    传送门 分析:感觉这道题有点意思.就写一篇mark一下吧. 现场比赛的时候去枚举了儿子用了线段树+dfs序,和预想的一样T了. 可以换一个想法,从儿子对父亲的贡献来思考. 在点中先假设一个节点的每一个 ...

  9. occ添加新的捕捉模式

    Load (theSelection, theShape, theType, theDeflection, theDeviationAngle, isAutoTriangulation, thePri ...

  10. github使用

    1.首先登录到https://github.com注册Github帐号,并且创建一个repository. 例如:注册的github帐号名为whu-zhangmin,创建的repository名称为w ...