1

I'm creating a bot for the Discord. I'm downloading a horoscope XML-file from the Internet. The XML-file structure is as follows:

<?xml version="1.0" encoding="utf-8"?>
<horo>
<date yesterday="04.01.2019" today="05.01.2019" tomorrow="06.01.2019"/>

<aries>
<yesterday>
Text 1
</yesterday>
<today>
Text 2
</today>
<tomorrow>
Text 3
</tomorrow>
</aries>

......

</horo>

I try to read it in javascript:

const fs = require('fs');
var HoroscopeData = new Object();

fs.readFile('./module/Horoscope.xml', 'utf8', function(err, data){

  if(err) {
    console.error("ERROR");
  }

  console.log("OK");
  HoroscopeData = data;
}

console.log(HoroscopeData);

In the console, I see the same thing that is in the XML-file

But I don't understand how to refer to "HoroscopeData" fields. How do I return a string which is in "aries->today"?

Shadow4571
  • 382
  • 2
  • 4
  • 10

1 Answers1

5

You could use a library like https://www.npmjs.com/package/xml-js

To convert the XML to JSON where you can access it using the built in javascript JSON functions JSON.parse(target) or JSON.stringify(target)

quoting this package, here is an example of how simple it is to turn XML into JSON

var convert = require('xml-js');
var xml =
'<?xml version="1.0" encoding="utf-8"?>' +
'<note importance="high" logged="true">' +
'    <title>Happy</title>' +
'    <todo>Work</todo>' +
'    <todo>Play</todo>' +
'</note>';
var result1 = convert.xml2json(xml, {compact: true, spaces: 4});
var result2 = convert.xml2json(xml, {compact: false, spaces: 4});
console.log(result1, '\n', result2);

If you are uncomfortable working directly in XML, this may help you because interacting with JSON is JavaScript is simple.

This is how to then extract what you need from the JSON that xml-js converts your XML to

var convert = require('xml-js');
var xml =
'<?xml version="1.0" encoding="utf-8"?>'+
'<horo>'+
'<date yesterday="04.01.2019" today="05.01.2019" tomorrow="06.01.2019"/>'+
'<aries>'+
'<yesterday>'+
'Text 1'+
'</yesterday>'+
'<today>'+
'Text 2'+
'</today>'+
'<tomorrow>'+
'Text 3'+
'</tomorrow>'+
'</aries>'+
'</horo>';

const rawJSON = convert.xml2json(xml, {compact: true, spaces: 4});
const convertedJSON = JSON.parse(rawJSON);
console.log(convertedJSON.horo.aries.today._text);
kjhughes
  • 106,133
  • 27
  • 181
  • 240
Happy Machine
  • 987
  • 8
  • 30
  • I converted XML to JSON. But still I do not understand how to refer to the element "aries->today". I tried like this: var ResultData = convert.xml2json(HoroscopeData, {compact: true, spaces: 4}); console.log(ResultData.horo.aries.today); "ResultData" is a string now? – Shadow4571 Jan 05 '19 at 16:55
  • We edited the post explains towards the bottom – Happy Machine Jan 05 '19 at 19:42
  • Thank you very much. I couldn't have done it without you. Now the more difficult part has begun. Async/Await =) – Shadow4571 Jan 05 '19 at 20:24
  • haha no problem, maybe you should check out this post i did a couple of days ago, happy to help with any questions, its horrible until it clicks! https://stackoverflow.com/questions/53992226/why-does-async-always-return-a-promise/53992403#53992403 – Happy Machine Jan 05 '19 at 21:17