Invalid character entity parsing xml

感情迁移 提交于 2019-12-10 13:27:04

问题


I am trying to parse a string of xml and I getting an error

[Error: Invalid character entity
Line: 0
Column: 837
Char:  ]

Does xml not like brackets? Do I need to replace all the brackets with something like \\]. Thanks


回答1:


Ok, the invalid character was the dash and an &. I fixed it by doing the following:

xml = data.testSteps.replace(/[\n\r]/g, '\\n')
                    .replace(/&/g,"&")
                    .replace(/-/g,"-");

Thanks




回答2:


Using a node domparser will get around having to do a string replace on every character that is not easily parsed as a string. This is especially useful if you have a large amount of XML to parse that may have different characters.

I would recommend xmldom as I have used it successfully with xml2js

The combined usage looks like the following:

var parseString = require('xml2js').parseString;
var DOMParser = require('xmldom').DOMParser;

var xmlString = "<test>some stuff </test>";
var xmlStringSerialized = new DOMParser().parseFromString(xmlString, "text/xml");
    parseString(xmlStringSerialized, function (err, result) {
        if (err) {
            //did not work
        } else {
            //worked! use JSON.stringify() 
            var allDone = JSON.stringify(result);
        }
    });


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!