Node XML parser in Max

Shaikat Hossain's icon

Hi, I'm currently trying to recreate the Sadam rapidxml object (which does not run on M1 macs) using Node Script in Max. I'm using the rapidx2j library in Node. Currently I'm having some confusion on how to properly pass the message into the parse function. An example XML message I may want to pass in would be:
load <?xml version=\"1.0\" encoding=\"UTF-8\"?> <black_russian> <ingredients> <ingredient name=\"vodka\" amount=\"5\" unit=\"cl\"> <brand name=\"Absolut\" country=\"Sweden\" base=\"wheat\"/> <brand name=\"Bols\" country=\"Netherlands\"/> <brand name=\"Finlandia\" country=\"Finland\" base=\"barley\"/> <brand name=\"Русский стандарт\" country=\"Russia\"/> </ingredient> <ingredient name=\"coffee liqueur\" amount=\"2\" unit=\"cl\"/> </ingredients> <preparation>Pour the ingredients into an old fashioned glass filled with ice cubes. Stir gently.</preparation> </black_russian>

The code I have so far is the following. I'm trying to pass in any message received (using the anything keyword) into the parse function but I get the error "XML" needs to be a string or a buffer." Is there a method to pass in a message as a string or buffer? Perhaps I'm not parsing these XML messages in the optimal way.

const x2j = require('rapidx2j');
const maxApi = require("max-api");

maxApi.addHandler('load', () =>{maxApi.post('test1')});    
    
const json = x2j.parse(maxApi.anything);
console.log(json);
Florian Demmer's icon

Looking at your code you are not "reacting" to the received input from your max message but instead attempting to parse the value of maxApi.anything which I assume is actually undefined.

You'd have to adjust the JS code to be executed in response to receiving the message, so eg something like this for a message parse "<xml_string>"

const x2j = require('rapidx2j');
const maxApi = require("max-api");

maxApi.addHandler('parse', (xml) =>{
  const json = x2j.parse(xml);
  console.log(json);
});