javascript - 在javascript中将xml数据转换为json
问题描述
需要帮助将 xml 响应数据转换为 json 值并从 JSON 对象中获取值。
我目前的代码是:
const soapRequest = require('easy-soap-request');
const fs = require('fs');
var convert = require('xml-js');
// example data
const url = 'https://sentro.com/ws/apf/ticketing/Gateway?WSDL';
const sampleHeaders = {
'user-agent': 'sampleTest',
'Content-Type': 'application/html;charset=UTF-8',
};
const xml = fs.readFileSync('Auto_query_request.txt', 'utf-8');
// usage of module
(async () => {
const { response } = await soapRequest({ url: url, headers: sampleHeaders, xml: xml, timeout: 1000 }); // Optional timeout parameter(milliseconds)
const { headers, body, statusCode } = response;
console.log(headers);
//console.log(body);
const options = {ignoreComment: true, alwaysChildren: true};
var result = JSON.parse(convert.xml2json(body, options));
console.log(result);
})();
当我执行上面的代码时,我得到如下响应。
{"declaration":{"attributes":{"version":"1.0","encoding":"UTF-8"}},"elements":[{"type":"element","name":"env:Envelope","attributes":{"xmlns:env":"http://schemas.xmlsoap.org/soap/envelope/","xmlns:wsa":"http://www.w3.org/2005/08/addressing"},"elements":[{"type":"element","name":"env:Header","elements":[{"type":"element","name":"wsa:MessageID","elements":[{"type":"text","text":"urn:5C8DC410D18D11EABFB75749E02F1482"}]},{"type":"element","name":"wsa:ReplyTo","elements":[{"type":"element","name":"wsa:Address","elements":[{"type":"text","text":"http://www.w3.org/2005/08/addressing/anonymous"}]},{"type":"element","name":"wsa:ReferenceParameters","elements":[{"type":"element","name":"instra:tracking.compositeInstanceCreatedTime","attributes":{"xmlns:instra":"http://xmlns.oracle.com/sca/tracking/1.0"},"elements":[{"type":"text","text":"2020-07-29T06:19:25.981-05:00"}]}]}]},{"type":"element","name":"wsa:FaultTo","elements":[{"type":"element","name":"wsa:Address","elements":[{"type":"text","text":"http://www.w3.org/2005/08/addressing/anonymous"}]},{"type":"element","name":"wsa:ReferenceParameters","elements":[{"type":"element","name":"instra:tracking.compositeInstanceCreatedTime","attributes":{"xmlns:instra":"http://xmlns.oracle.com/sca/tracking/1.0"},"elements":[{"type":"text","text":"2020-07-29T06:19:25.981-05:00"}]}]}]}]}
有了上述结果,我很难从 JSON 对象中获取数据。
所以我需要用正确的 JSON 对象转换这个 xml 主体,如下所示。
{
"Header": {
"MessageID": "urn:45597DC0D1B511EA8F1C35236A977E2C",
"ReplyTo": {
"Address": "http://www.w3.org/2005/08/addressing/anonymous",
"ReferenceParameters": {
"tracking.compositeInstanceCreatedTime": "2020-07-29T11:05:06.880-05:00"
}
},
"FaultTo": {
"Address": "http://www.w3.org/2005/08/addressing/anonymous",
"ReferenceParameters": {
"tracking.compositeInstanceCreatedTime": "2020-07-29T11:05:06.880-05:00"
}
}
},
"Body": {
"processResponse": {
"payload": {
"RfcProject": {
"IntegrationStatus": "Query - Success",
"Id": "something query",
"Created": "2020-06-16T10:24:18",
"CreatedBy": "something",
"Updated": "2020-07-23T14:14:03",
"UpdatedBy": "somevalue",
"ProjectNum": "something",
以及如何从 JSON 值中获取值。任何人都可以在这里提供帮助
解决方案
推荐阅读
- java - 在 WSDL4J 中将一个部分添加到另一部分中
- ffmpeg - ffmpeg:“未找到引用的 QT 章节轨道”
- c# - c#与取消注释被调用方法中的所有内容相比,取消注释调用方法时的行为差异
- perl - perl 匹配特定的数字和单词
- javascript - 如何处理在 puppeteer 中的 ajax 请求后加载的元素
- jquery - 从特定值jquery获取所有数组信息
- apache-kafka - 加入Kafka流中的外键
- android - Firebase 应用内消息:窗口全局管理器和图像视图操作问题
- javascript - Vue方法在其他方法中不起作用
- javascript - 具有序数值的 D3 滑块卡在第 8 行