Facility Control

 View Only
  • 1.  Parsing an XML string (not a file)

    Posted 05-01-2019 19:50

    Working on some control for a 3rd party device. 

    I'm using an asyncPost command to login to the device, and the device will respond with a token I need to include in further commands. The device resopnds in XML format and I need to parse this. It seems like the XML parse function built into ogscript is for XML files only, where this is a variable being delivered to my callback function. What is the best way to go about this?



  • 2.  RE: Parsing an XML string (not a file)

    Posted 05-01-2019 19:51

    I have a feeling the forum will not like XML code but I'll try. Here is the response from the device:

     

    <?xml version="1.0" encoding="UTF-8"?><api_response><version>6</version><timestamp>2019-05-01 20:39:27</timestamp><success>1</success><token>qau14rbel049tql9d7pvi00ri6</token></api_response>

    I successfully have this in a variable but i need to parse to extract the token and check success flag.


    #DashBoard


  • 3.  RE: Parsing an XML string (not a file)

    Posted 05-02-2019 20:10

    You can pass the result directly into the ogscript.parseXML function - it will take a String, a URL, or a file path:

     

    ////////// THIS WOULD ACTUALLY BE THE RESPONSE FROM YOUR HTTP POST
    var resp = '<?xml version="1.0" encoding="UTF-8"?><api_response><version>6</version><timestamp>2019-05-01 20:39:27</timestamp><success>1</success><token>qau14rbel049tql9d7pvi00ri6</token></api_response>';
    //////////

    var doc = ogscript.parseXML(resp);
    var nodes = doc.getElementsByTagName('token');
    if (nodes != null && nodes.getLength() > 0)
    {
    ogscript.debug("TOKEN: " + nodes.item(0).getTextContent());
    }

    #DashBoard