Non-whitespace before first tag in xmls2js
Below code i am running and trying to parse XML response through xmls2js,
soapClient.call({ 'method':'findAll', 'params' : { 'maxResults' : 100, 'orderDirection' : 0, 'orderField' : 'creationdate', 'startIndex' :1 } } ) .then(function(res) { console.log(res.response); res.data // response data as array var data=res.response // full response data (including xml) res.header // response header
parseString(data, function(err, result){
if(err) console.log(err);
console.log(result);
});
},
function(err) {
console.log(err);
}
);
While running the code getting below error.
Error: Non-whitespace before first tag.
How does the XML you pass in look like?
This blog solved it for me.
This should work out of the box, since xml2js has now code to remove the byte order mark before parsing.
@fxlemire I am facing the same issue and the blog link is not working.
Please provide a solution. Thank you!.
@lucifer-AM To fix the issue:
var cleanedString = origString.replace("\ufeff", "");
Having same error.
and var cleanedString = origString.replace("\ufeff", ""); didnt work for me.
attached sample xml
@fxlemire I have a feed Reader that I created for RDF, ATOM as well as RSS. It works fine for all three but for some feeds it give this error. Error: Non-whitespace before first tag. Currently I think it is related to the HTTP response that I am getting after I hit the feed URL. As the response is garbled I can't give it to the xml2js. Currently I am avoiding the erroneous feed urls. Thanks anyways.
Any fix?
I had the same issue, but then I realized I was trying to parse the entire response object of an AJAX request instead of just the response data.
I have the same issue. Any solutions?
Try this... fs = require('fs'); var parseString = require('xml2js').parseString;
fs.readFile('../storage/epg/epg.xml', function(err, data) { var data = data.toString().replace("\ufeff", ""); parseString(data, function (err, result) { console.dir(result); }); });
XML files are utf8 and when stored as a file should have a binary header ("\ufeff"). The previous comment solve this by removing it before passing it. An alternative is to use fs.readfile which will deal with the header.
fs.readFile('../simple test.xml', function(err, data) {
var doc = new xmldoc.XmlDocument(data);
Thanks everyone for sharing your solutions and to the authors of this library for creating it! This thread was helpful to me.
In case it helps someone else, here's a full example, going from xml file with binary headers to writing the results to a file.
const fs = require('fs');
const util = require('util')
const parseString = require('xml2js').parseString;
const fileToParse = ('./my-file.xml')
const outputFilename=('output.json')
fs.readFile(fileToParse, function (err, data) {
parseString(data, function (err, result) {
console.dir(result);
if (err) {
console.log(err)
}
// do something with the JS object called result, or see below to save as JSON
fs.writeFile('output.json', JSON.stringify(result), (writeErr) => {
if (writeErr) throw writeErr;
console.log('The file has been saved to ' + outputFilename);
});
});
});
If you try to write the JS object straight to a file, you get [Object object] in the file, so I converted to JSON.
I had the same issue, but then I realized I was trying to parse the entire response object of an AJAX request instead of just the response data.
fk me
I used a try and catch block, worked perfectly for me.
@justintemps, you just saved me, thank you so much!! haha
Try passing
{
preserveWhitespace: true
}
In options when instantiating the client.
This may be overkill or an obtuse approach but this did the trick for my https server receiving XML webhooks. This snippet depends on express and I'm using DOMParser from xmldom to clean the data before it is parsed.
app.post("/test", (req, res) => {
let body = '';
req.setEncoding('utf8');
req.on("data", (chunk) => {
body += chunk;
});
req.on('end', () => {
const tempData = body.toString();
try {
const cleanData = new DOMParser().parseFromString(tempData, "text/xml");
return parseString(cleanData, (err, result) => {
if (err) {
console.log(err)
res.statusCode = 400;
return res.end("Error 2: " + err)
} else {
console.log(result);
res.statusCode = 200;
return res.end("Success.");
}
});
} catch (err) {
console.log(err);
res.statusCode = 400;
return res.end("Error 1: " + err);
}
});
});
Console:

Ngrok inspection:

I had the same issue, but then I realized I was trying to parse the entire response object of an AJAX request instead of just the response data.
Same to me, I'm feeling very stupid right now hahahaha Thanks.