0

I am using below code to fetch some value from response body. Currently this is the response body is returning the value as in xml format. Now i need to convert this response body into html or json format.

Can any one help to convert this

const soapRequest = require('easy-soap-request');
const fs = require('fs');

// example data
const url = 'https://cap.com/ws/apf/ticketing/?WSDL';
const sampleHeaders = {
  'user-agent': 'sampleTest',
  'Content-Type': 'application/html;charset=UTF-8',
  'soapAction': 'https://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#LatLonListZipCode',
};
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);
  console.log(statusCode);
})();

result:

{
  date: 'Wed, 29 Jul 2020 08:45:56 GMT',
  'content-type': 'text/xml; charset=utf-8',
  'transfer-encoding': 'chunked',
  connection: 'close',
  'content-language': 'en'
}

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing"><env:Header><wsa:MessageID>urn:EB6ACD60D17711EA8F1A35236A977E2C</wsa:MessageID><wsa:ReplyTo><wsa:Address>
So and so data

200
Vijay
  • 27
  • 6
  • This might help [https://stackoverflow.com/questions/1773550/convert-xml-to-json-and-back-using-javascript](https://stackoverflow.com/questions/1773550/convert-xml-to-json-and-back-using-javascript) – Ludovit Mydla Jul 30 '20 at 06:29

1 Answers1

0

To convert xml to json or js object us xml-js - https://www.npmjs.com/package/xml-js

Something like -

const convert = require('xml-js');

const options = {ignoreComment: true, alwaysChildren: true};

const xml = '<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing"><env:Header><wsa:MessageID>urn:EB6ACD60D17711EA8F1A35236A977E2C</wsa:MessageID><wsa:ReplyTo><wsa:Address>'

let result = convert.xml2js(xml, options); // or convert.xml2json(xml, options)
Spiderman
  • 330
  • 1
  • 8
  • ```const options = {ignoreComment: true, alwaysChildren: true}; var result1 = convert.xml2json(body, options);``` I am passing the body like this to convert to json format. Is this ok? .. Also once i convert into json . how to retrieve particular value – Vijay Jul 29 '20 at 11:39
  • if you want to use ```xml2json``` then you must use ```JSON.pares(convert.xml2json(body, options))``` where as ```convert.xml2js(xml, options);``` does that for you already. – Spiderman Jul 29 '20 at 13:21
  • On a side note, you XML is not proper according to ```xml-js``` – Spiderman Jul 29 '20 at 13:21