Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Every single XML parser I tried doesn't work with this specific XML

index.js

const fs = require("node:fs")
const xml2js = require('xml2js')

const parser = new xml2js.Parser()

const data = fs.readFileSync('test.xml', "utf8")
const json = JSON.parse(await parser.parseStringPromise(data))

console.log(JSON.stringify(json))

test.xml

<thing>
    <2 value="300.0"/>
    <3 value="1.0"/>
</thing>

All of this only returns this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error: Unencoded <
Line: 1
Column: 3
Char: 2

I’ve been stuck trying to figure out what is wrong for hours straight, I tried 3 xml2json, xml2js and xml-js. None of them worked and they return the same error.

>Solution :

Your XML is invalid. An XML element name cannot start with a number.

https://www.w3.org/TR/REC-xml/#sec-common-syn

The first character of a Name must be a NameStartChar, and any other characters must be NameChars; this mechanism is used to prevent names from beginning with European (ASCII) digits or with basic combining characters. Almost all characters are permitted in names, except those which either are or reasonably could be used as delimiters. The intention is to be inclusive rather than exclusive, so that writing systems not yet encoded in Unicode can be used in XML names. See J Suggestions for XML Names for suggestions on the creation of names.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading