将XML转换为JSON以提取数据,然后在Postman中将JSON转换回XML [英] Convert XML to JSON for extracting data then convert JSON back to XML in Postman

查看:853
本文介绍了将XML转换为JSON以提取数据,然后在Postman中将JSON转换回XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名初级测试人员,并且我使用邮递员将xml帖子链接在一起。
我已经在使用全局变量和环境变量方面取得了成功,但是在提取一个孩子及其子孩子然后将整个块放入我的下一篇文章时遇到了问题。例如
我已经收到以下

I am a junior tester and I am using postman to chain together xml posts. I have already had success with using global and environmental variables, however I am having a problem with extracting a child and its sub-child and then putting the whole chunk into my next post. for example I have received the following

<?xml version="1.0" encoding="UTF-8"?>
<Example data="example"/>
<Demonstration filler="231" moreExample="true"/>

<Target>
  <TargetSegment lettuce="true" onion="99" pickles="absolutely"/>
     <TargetSubSegment potatoe="true"/>
  </TargetSegment>
  <TargetSegment chives="true" cabbage="99" celery="absolutely"/>
     <TargetSubSegment turnip="true"/>
  </TargetSegment>
</Target>

<Example2 data="example"/>
<Demonstration2 filler="231" moreExample="true"/>

我该如何获取< Target> 并将其自动放入我的下一个帖子正文中?
如果您需要更多信息,请让我知道,因为这是我第一次发布,并且我仍在学习邮递员,基本上是所有东西。

How can i take everything inside of <Target> and have that automatically placed into my next post body? If you need any more information please let me know as this is my first time posting and I am still learning postman, and basically everything.

我已经尝试过做JSON.stringify(variableName)(在我将xml转换为Json之后),我对此没有任何运气。

I have tried to do do JSON.stringify(variableName) (after I had converted xml to Json) and I didnt have any luck with that.

推荐答案

<坦率地说,欢迎您使用Stackoverflow! :D

Frank, welcome to Stackoverflow! :D

您在此处发布的XML似乎不太有效(已使用工具进行了检查),只是对其进行了测试并为您提供了在下面的示例中,我将您的XML包装在 Data 标记中以使其有效。

The XML that you've posted here doesn't quite seem to be valid (checked it with a tool) and just to test it out and give you the following example, I wrapped your XML in a Data tag to make it valid.

这是您的一个集合可以在邮递员应用中导入 https://www.getpostman.com/collections/d3f003de1376dc5d042b

This is a collection that you can import in your postman app: https://www.getpostman.com/collections/d3f003de1376dc5d042b

说明:
我想您已经在使用 xml2json 内置在邮递员应用中的库。如果您还没有使用过它,那么肯定要检查它,因为它是您的案例所必需的。

Explanation: I guess you're already using the xml2json library that's built into the postman app. If you haven't used it yet then definitely check it out since it's required for your case.

现在,一旦导入了集合,您将在第一个窗口中看到它。要求我写一些评论让您了解发生了什么。
您可以将xml转换为json,提取目标的数据并将其存储在变量中。

Now, once you've imported the collection you can see in the first request that I've written some comments for you to understand what's happening. You can convert the xml to json, extract out the target's data and store that in a variable.

在下一个请求(POST请求)中,您可以

In the next request (POST Request), you can refer to the environment variable inside the request body.

您总是可以联系邮递员社区

更新: em>

Update:

我已经更新了收藏集,并不得不生成一个新链接(使用以下链接再次导入):
https://www.getpostman.com/collections/7660a47bc6fedaf5c5b7

I have updated the collection and had to generate a new link (Import it again using the following link): https://www.getpostman.com/collections/7660a47bc6fedaf5c5b7

我能够找到一个将 json-to-xml 转换的脚本,希望对您有所帮助。
检查集合的Pre-request脚本和POST请求的请求正文。

I was able to find a script which converts json-to-xml and I hope it helps you out. Check the Pre-request script of the collection and the request body of the 'POST' request.

只需转换存储在环境中的JSON数据变量返回到XML并将其存储在新的环境变量中。
然后在请求正文中引用该环境变量。

Just converting the JSON data that was stored in the environment variable back to XML and storing it in a new environment variable. Then referring that environment variable in the request body.

脚本源(json2xml): http://goessner.net/download/prj/jsonxml/

Script source (json2xml): http://goessner.net/download/prj/jsonxml/

这篇关于将XML转换为JSON以提取数据,然后在Postman中将JSON转换回XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆