jgravois / koop-walmart

fetch walmart georss and display it in ArcGIS Online
Other
2 stars 3 forks source link

Error: Unencoded < Line: 0 Column: 2 when parsing xml from remote service #1

Closed jgravois closed 7 years ago

jgravois commented 7 years ago

I feel like i'm close with this Walmart provider to help the disaster response team but I’m having trouble parsing the .xml returned by the feed.

Initially, there were a couple bogus characters at the beginning of the body that needed trimmed. Afterward, even though the input looked valid, the strict xml2js/sax-js parser complained

Error: Unencoded <
Line: 0
Column: 2

stepping through that code i realized it is choking on "" characters between the characters that are actually visible in the browser developer tools

example:

const walmartRss = '<rss xmlns:georss="http://www.georss.org/georss" ...'

walmartRss.charAt(0)
<
walmartRss.charAt(1) // parser sees this as ""

walmartRss.charAt(2)
r
walmartRss.charAt(3) // parser sees this as ""

walmartRss.charAt(4)
s

for the moment, i'm stumped.

@nixta @dmfenton @tomtom92 if any of you are interested/available to take a look, it should be as straightforward as

  1. cloning this repo
  2. npm install && npm start
  3. visiting http://localhost:8080/sample/FeatureServer/0/query
jgravois commented 7 years ago

figured it out!

const request = require('request')
const iconv  = require('iconv-lite')
const parseString = require('xml2js').parseString

request({ 
    uri: 'https://walmart.alertlink.com/rss/stores.rss',
    gzip: true,
    encoding: null,
    headers: {
        "Accept-Encoding": "gzip, deflate, br"
    }
}, (err, res, body) => {
    if (err) return callback(err)
    // response is encoded as utf16. who knew?!?
    // i sure didn't. i barely understand what that even means!
    const rss = iconv.decode(new Buffer(body), "utf16");

    parseString(rss, function (err, result) {
        console.log(result)
    })
})