Processing XML in Worker

From reading posts, it looks like DOMParser is not possible inside worker? How do you processing XML API calls? Is there any examples of using Worker with XML?

Specific, my worker need to make an external API calls. The API response is in XML. I will need to read values of specific nodes in the returned XML and generate contents based on the XML. This XML is not ATOM/RSS. Just plain XML. So far I find it very difficult to process XML file in Worker. Pretty much all the node modules I am trying to use has issues with worker. libxmljs has a fs binding issue. If anyone know how to make it work with worker would be great.

I had to do this just a little bit ago for my Cloudflare Status Worker. I used the npm library htmlparser2 (Link here)

You can find my repo here: GitHub - WalshyDev/cf-status-worker: Cloudflare Worker to monitor the Status Pages for incidents and send notifications to Discord

Thank you
It looks like I could run parseFeed, but when I want to start playing with actual XML nodes, I have no way to do that.

Anyone was able to fetch an XML file and select the nodes they need?

What do you want to do exactly? You run parseFeed and then just mess with the JSON as you wish

I recommend you join the Discord, there’s a lot of members over there and we can help a lot more quickly. Invite: Discord

parseFeed give me null because the in the documentation, parseFeed is used to process RSS/RDF/Atom Feeds

Yeah there isn’t just parseFeed in there, parseDocument is probably more for you - htmlparser2 - npm

Or you could find a different npm module but that would definitely be the way to go right now is a module. If you want other recommendations in Discord because I know quite a few in there have done XML parsing too.

1 Like

I got this error when I use parseDocument. Basic what I do is fetch xml file from API call and then
var xmlDoc = parseDocument(xml);

TypeError: this.buffer.substr is not a function
at t.cleanup (worker.js:1:21667)
at t.parse (worker.js:1:24407)
at t.write (worker.js:1:13339)
at t.end (worker.js:1:13451)
at t.end (worker.js:1:11578)
at u (worker.js:1:70351)
at worker.js:1:71257