0

I've tried to use xml-js and xml2js but both are returning the same error.

<?xml version="1.0" encoding="UTF-8"?>
^

SyntaxError: Unexpected token <
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)

The xml example:

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

The code example:

var notes =require('./test.xml')

var convert = require('xml-js');
var xml =notes;

var result1 = convert.xml2json(xml, {compact: true, spaces: 4});
var result2 = convert.xml2json(xml, {compact: false, spaces: 4});

console.log(result1, '\n', result2);

Can you help with the solution to this? I've tried using JSON.parse, JSON.stringify but it is not working anyways.

I'm trying to parse an XML to convert to JSON, by the way.

spaceman
  • 659
  • 2
  • 11
  • 29

1 Answers1

5

The issue isn't with the XML parsing, it's with the fact you're using require, which loads Node.js module for reading another file. Instead, you should use a simple file reading method, such as fs.readSync:

fs = require('fs');
var notes = fs.readFileSync('./test.xml')
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    @spaceman: Recheck your work: `readFileSync()` would not yield the same error. – kjhughes Mar 30 '19 at 16:33
  • @spaceman please post the full code and the error you're getting. Your snippet has a mistake later on where you use `mNotes` instead of `notes`, but other than that, it works on my machine. – Mureinik Mar 30 '19 at 16:33
  • @Mureinik the error and the code is in the question, didn't change. Ive corrected this mistake you've noticed minute after posting it. – spaceman Mar 30 '19 at 16:36
  • var fs = require('fs') || var xml = fs.readFileSync('./test.xml') || console.log(xml) – spaceman Mar 30 '19 at 16:38
  • @spaceman that's a completely different error. You can't use `||` like that, just separate those three expressions to three different statements with semicolons (`;`s). – Mureinik Mar 30 '19 at 16:39
  • Just added the code and error using readfilesync to the question – spaceman Mar 30 '19 at 16:40
  • @spaceman are you **sure** that's the code you're executing? it works fine on my machine (albeit it prints out a somewhat useless buffer) – Mureinik Mar 30 '19 at 16:41
  • @Mureinik hahaha, `||` was just to post in the comments area. Let's assume we all know the basics of programming – spaceman Mar 30 '19 at 16:41
  • 1
    @spaceman: It's not about the `||`. It's about you failing to extract a true [mcve]. Until you do that, we can't help you without guessing about the rest of your context, and that's extremely inefficient. – kjhughes Mar 30 '19 at 16:41
  • @kjhughes looooooooooooooooooooooool good saturday for you, mate. You misunderstood. My question was posted with a minimal, complete and verifiable example. If you are not coming to help, please don't mess with the question – spaceman Mar 30 '19 at 16:45
  • 1
    @spaceman: The three lines of code alone you posted with `readFileSync()` simply do not yield the error you posted. You're wasting everyone's time by not posting a ***complete*** MCVE. Until you narrow the problem properly, you'll have a hard time figuring out your problem or getting help from others. Moving on. Good luck. – kjhughes Mar 30 '19 at 16:47