首页 > 解决方案 > 在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 值中获取值。任何人都可以在这里提供帮助

标签: javascriptnode.js

解决方案


推荐阅读