How to read XML file and pass to JS object


    Jun 05 2012 | 2:54 pm
    I am rather new to Max and have been pretty successful so far, however I am now trying to branch out a bit.
    What I am trying to do is: -Read a XML file (currently I am using the 'text' object) -Pass the files contents as a string to a 'js' object (I have been trying messages and using 'prepend' to add a function to the beginning..) note: the bang message is received and works
    my JS test script: ===================================
    autowatch = 1; // allow Max to watch for changes.
    inlets = 1; outlets = 1;
    function bang(){ post("JS is working n"); // includes new line char }
    function msg_string(stringArg){ post("your string" + stringArg); post(); // create new line }
    ===================================

    • Jun 05 2012 | 4:56 pm
      Hi,
      unless you have some explicit reason to process the XML file in JS, you could use the XML processing tools which I created for Max. The [sadam.dom] and [sadam.sax] objects work with MXJ and they implement a complete W3C-compliant DOM and SAX parser. There's also a native object called [sadam.rapidXML] which is not 100% W3C-compliant (mainly because it ignores DOCTYPE declarations), but is very fast. They are part of my library which you can get for free from http://www.sadam.hu/software .
      Hope this helps, Ádám
    • Jun 05 2012 | 6:44 pm
      Thanks, Adam I have infact looked at your tools, and they seem to be very good. The one thing that turned me away was the non-commercial license. Although I am very far away from a commercial product (just starting) that is my goal, therefore I do not want to build upon a component that is not available for this purpose. Do you offer a commercial version?
    • Jun 07 2012 | 10:12 am
      Hi dusx,
      I've sent you a message regarding the possibilities of a commercial version.
      Best, Ádám
    • Dec 14 2012 | 6:57 am
      Hi dusx,
      Have been trying to do the same thing with the [js] object. Tried several libraries with no luck, but did find a workaround:
      1. Use aka.shell to convert the xml file to a JSON file using this python script: https://github.com/hay/xml2json - (note you'll need to change the json 'import' statement to: import json as simplejson, optparse, sys, os) and the actual command line syntax is: xml2json.py -t xml2json file.xml > file.json. Or you can use any other command line xml to json converter.
      2. Then read in the JSON file with the [js] object using the patch from Peter Nyoeber (near the bottom of the following post): https://cycling74.com/forums/sharing-is-fun-example-write-and-read-json-in-javascript
      You can then access all the data using JSON format inside the [js] object, which is about a thousand times easier than parsing xml anyway.
      Note: If you're using http: requests to get xml data via [jit.uldl] - you can write it out to an xml file using [jit.textfile]. See the CNMAT weather report example. Or let me know and I can post an example.
      Hope this helps.
      tz