xml-js icon indicating copy to clipboard operation
xml-js copied to clipboard

Invalid character in entity name

Open seanmcilvenna opened this issue 6 years ago • 12 comments

I am getting an error when parsing a valid XML document:

Error: Invalid character in entity name
Line: 2
Column: 10124
Char:  
    at error (C:\Users\sean.mcilvenna\Code\FHIR.js\node_modules\sax\lib\sax.js:651:10)
    at strictFail (C:\Users\sean.mcilvenna\Code\FHIR.js\node_modules\sax\lib\sax.js:677:7)
    at SAXParser.write (C:\Users\sean.mcilvenna\Code\FHIR.js\node_modules\sax\lib\sax.js:1491:13)
    at Object.module.exports [as xml2js] (C:\Users\sean.mcilvenna\Code\FHIR.js\node_modules\xml-js\lib\xml2js.js:337:12)

The problem appears to be coming from the & in the following XML snippet:

      <mapping>
        <identity value="rim"/>
        <map value="status Amended &amp; Final are differentiated by whether it is the subject of a ControlAct event with a type of &quot;revise&quot;"/>
      </mapping>

However, this snippet is valid XML... Can you confirm that this is indeed a bug?

seanmcilvenna avatar Jul 31 '18 20:07 seanmcilvenna

Same thing I wanted to write it donw

xml = '<a wh="as&d@{*as">what</a>'
var result1 = convert.xml2json(
  xml, {
    compact: true, 
    spaces: 4,
    ignoreAttributes: true,
    attributeValueFn: (attrValue) => entities.encodeXML(attrValue)
  });

baltuonis avatar Jan 04 '19 16:01 baltuonis

I have the same problem when parsing a Vue Component:

<template><hr v-if="config && config.var === 'val'" /></template>

LukasBombach avatar Mar 14 '19 15:03 LukasBombach

Hi @nashwaan, any comment on this, or #80? It looks like if XML includes & then xml-js is searching for ; and is testing everything between.

Problem is https://github.com/isaacs/sax-js

if (isNaN(num) || numStr.toLowerCase() !== entity) {
   strictFail(parser, 'Invalid character entity')
   return '&' + parser.entity + ';'
}

or better true strict mode in xml2js.js (strict - Boolean. Whether or not to be a jerk. Default: false.)

var parser = pureJsParser ? sax.parser(true, {}) : parser = new expat.Parser('UTF-8');

Result:
/node_modules/sax/lib/sax.js:968
      throw this.error
      ^

Error: Invalid character entity

=>

var parser = pureJsParser ? sax.parser(false, {}) : parser = new expat.Parser('UTF-8');

Result:
OK!

I think there should be an option to enable/disable this "strict mode".

JonasJonny avatar Apr 11 '19 21:04 JonasJonny

@nashwaan We really need to have an option how to change true to false.

JonasJonny avatar Jun 28 '19 22:06 JonasJonny

anyone working on this?

cthorner avatar Jan 02 '20 21:01 cthorner

I would like to know the same :)

seanmcilvenna avatar Jan 02 '20 23:01 seanmcilvenna

Was this ever solved?

damianof avatar May 07 '20 19:05 damianof

Not to my knowledge.

seanmcilvenna avatar May 07 '20 20:05 seanmcilvenna

CC: @nashwaan

JonasJonny avatar May 09 '20 10:05 JonasJonny

Alternatives, please!!!

macCesar avatar Jul 18 '20 18:07 macCesar

Was this ever solved?

prb407 avatar Aug 24 '20 08:08 prb407

https://stackoverflow.com/questions/24877085/invalid-character-entity-parsing-xml

I used this answer and it worked

elron avatar Dec 15 '23 10:12 elron