Facility Control

 View Only
  • 1.  Dynamic dropdown from XML data

    Posted 06-18-2020 20:35

    Hello all,

    Although I have successfully been able to parse through some XML files, and I have reviewed all the XML example code on this forum, I have a challenge I have not been able to solve yet. In our environment, we have a series of long XML files containing name data that will be used to generate lower thirds.

    I would like to have just the name from each section of the XML tree populate a drop box e.g. </ArrayOfPersonData/PersonData[1]/PersonName> such that a user could select a name from the dynamically generated droplist, which may have 50 names in it.

    Upon selection of the name, I'd like to return the index of the dropbox, so I could use that to run a function which will pull each of the associated fields for that PersonData portion of the XML tree, e.g. /PersonData[2]/TitleLine1 for the second person in the list. I know how to do the latter portion, but I need a bit of help filling the dropdown, and returning the dropdown index as an integer I can use to formulate my followup XML query. As the /PersonData parts of the XML effectively increase in number with each person in the file, the index return would correspond to the correct person data in my new query.

    Here's a sample of the XML name data below. I have found I needed to use XPath to be able to parse these files, probably due to the formatting style, which I cannot change. If anyone can help point me in the right direction, I would be very grateful!


    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfPersonData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <PersonData>
    <MacroName />
    <MicNumber />
    <PersonName>Sarah Sander</PersonName>
    <Honorific />
    <HonorificEng />
    <HonorificFre />
    <Salutation>Ms. / Mme</Salutation>
    <SalutationEng>Ms.</SalutationEng>
    <SalutationFre>Mme</SalutationFre>
    <Riding />
    <OrganizationLine1>Big Company</OrganizationLine1>
    <OrganizationLine2></OrganizationLine2>
    <OrganizationLine3 />
    <OrganizationLine4 />
    <TitleLine1>Someplace County</TitleLine1>
    <TitleLine2>Someplace County</TitleLine2>
    <TitleLine3 />
    <TitleLine4 />
    <Videoconference />
    <Background>C:\Graphics\Backgrounds\noparty.png</Background>
    <LastFirstName>Sander, Sarah</LastFirstName>
    </PersonData>
    <PersonData>
    <MacroName />
    <MicNumber />
    <PersonName>Firstname Lastname</PersonName>
    <Honorific />
    <HonorificEng />
    <HonorificFre />
    <Salutation>Mr. / M.</Salutation>
    <SalutationEng>Mr.</SalutationEng>
    <SalutationFre>M.</SalutationFre>
    <Riding />
    <OrganizationLine1>Big Company</OrganizationLine1>
    <OrganizationLine2></OrganizationLine2>
    <OrganizationLine3 />
    <OrganizationLine4 />
    <TitleLine1>Chief Information Officer</TitleLine1>
    <TitleLine2>dirigeant principal de l'information</TitleLine2>
    <TitleLine3 />
    <TitleLine4 />
    <Videoconference />
    <Background>C:\Graphics\Backgrounds\noparty.png</Background>
    <LastFirstName>Lastname, Firstname</LastFirstName>
    </PersonData>



  • 2.  RE: Dynamic dropdown from XML data

    Posted 06-19-2020 15:14

    Hi Dave

    This code runs the necessary XPath on your XML and creates the new constraint for a dropdown list that you can use:

    var doc = ogscript.parseXML(xml);
    var nodeList = ogscript.runXPath('/*/PersonData', doc);

    ogscript.putObject("person-data", nodeList);

    var newConstraint = [];

    for (var i = 0; i < nodeList.getLength(); i++)
    {
    newConstraint.push(nodeList.item(i).getElementsByTagName("TitleLine1").item(0).getTextContent());
    }

    newConstraint.push({"key": -1, "value":"NO SELECTION"});

    params.replaceConstraint('List', params.createIntChoiceConstraint(newConstraint));

     

    If you want to see the whole thing in motion, here it is in a custom panel:

    <abs contexttype="opengear" keepalive="true">
    <meta>
    <params>
    <param access="1" constrainttype="INT_CHOICE" name="List" oid="List" precision="0" type="INT32" value="-1" widget="combo">
    <constraint key="-1">NO SELECTION</constraint>
    </param>
    </params>
    </meta>
    <param expand="true" height="45" left="124" oid="List" runtasksonload="true" top="170" width="316">
    <task tasktype="ogscript">var dataDisplay = "";

    var value = this.getValue();
    if (value &gt;= 0)
    {
    var nodeList = ogscript.getObject("person-data");
    if (nodeList != null &amp;&amp; value &lt; nodeList.getLength())
    {
    var node = nodeList.item(value);
    dataDisplay = node.getElementsByTagName("PersonName").item(0).getTextContent();
    }
    }

    ogscript.rename("data-display", dataDisplay);</task>
    </param>
    <button buttontype="push" height="52" left="449" name="Load" top="167" width="142">
    <task tasktype="ogscript">var xml = '&lt;?xml version="1.0" encoding="utf-8"?&gt;\n' +
    '&lt;ArrayOfPersonData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;\n' +
    '&lt;PersonData&gt;\n' +
    '&lt;MacroName /&gt;\n' +
    '&lt;MicNumber /&gt;\n' +
    '&lt;PersonName&gt;Sarah Sander&lt;/PersonName&gt;\n' +
    '&lt;Honorific /&gt;\n' +
    '&lt;HonorificEng /&gt;\n' +
    '&lt;HonorificFre /&gt;\n' +
    '&lt;Salutation&gt;Ms. / Mme&lt;/Salutation&gt;\n' +
    '&lt;SalutationEng&gt;Ms.&lt;/SalutationEng&gt;\n' +
    '&lt;SalutationFre&gt;Mme&lt;/SalutationFre&gt;\n' +
    '&lt;Riding /&gt;\n' +
    '&lt;OrganizationLine1&gt;Big Company&lt;/OrganizationLine1&gt;\n' +
    '&lt;OrganizationLine2&gt;&lt;/OrganizationLine2&gt;\n' +
    '&lt;OrganizationLine3 /&gt;\n' +
    '&lt;OrganizationLine4 /&gt;\n' +
    '&lt;TitleLine1&gt;Someplace County&lt;/TitleLine1&gt;\n' +
    '&lt;TitleLine2&gt;Someplace County&lt;/TitleLine2&gt;\n' +
    '&lt;TitleLine3 /&gt;\n' +
    '&lt;TitleLine4 /&gt;\n' +
    '&lt;Videoconference /&gt;\n' +
    '&lt;Background&gt;C:\Graphics\Backgrounds\noparty.png&lt;/Background&gt;\n' +
    '&lt;LastFirstName&gt;Sander, Sarah&lt;/LastFirstName&gt;\n' +
    '&lt;/PersonData&gt;\n' +
    '&lt;PersonData&gt;\n' +
    '&lt;MacroName /&gt;\n' +
    '&lt;MicNumber /&gt;\n' +
    '&lt;PersonName&gt;Firstname Lastname&lt;/PersonName&gt;\n' +
    '&lt;Honorific /&gt;\n' +
    '&lt;HonorificEng /&gt;\n' +
    '&lt;HonorificFre /&gt;\n' +
    '&lt;Salutation&gt;Mr. / M.&lt;/Salutation&gt;\n' +
    '&lt;SalutationEng&gt;Mr.&lt;/SalutationEng&gt;\n' +
    '&lt;SalutationFre&gt;M.&lt;/SalutationFre&gt;\n' +
    '&lt;Riding /&gt;\n' +
    '&lt;OrganizationLine1&gt;Big Company&lt;/OrganizationLine1&gt;\n' +
    '&lt;OrganizationLine2&gt;&lt;/OrganizationLine2&gt;\n' +
    '&lt;OrganizationLine3 /&gt;\n' +
    '&lt;OrganizationLine4 /&gt;\n' +
    '&lt;TitleLine1&gt;Chief Information Officer&lt;/TitleLine1&gt;\n' +
    '&lt;TitleLine2&gt;dirigeant principal de l\'information&lt;/TitleLine2&gt;\n' +
    '&lt;TitleLine3 /&gt;\n' +
    '&lt;TitleLine4 /&gt;\n' +
    '&lt;Videoconference /&gt;\n' +
    '&lt;Background&gt;C:\Graphics\Backgrounds\noparty.png&lt;/Background&gt;\n' +
    '&lt;LastFirstName&gt;Lastname, Firstname&lt;/LastFirstName&gt;\n' +
    '&lt;/PersonData&gt;&lt;/ArrayOfPersonData&gt;\n';

    var doc = ogscript.parseXML(xml);
    var nodeList = ogscript.runXPath('/*/PersonData', doc);

    ogscript.putObject("person-data", nodeList);

    var newConstraint = [];

    for (var i = 0; i &lt; nodeList.getLength(); i++)
    {
    newConstraint.push(nodeList.item(i).getElementsByTagName("TitleLine1").item(0).getTextContent());
    }

    newConstraint.push({"key": -1, "value":"NO SELECTION"});

    params.replaceConstraint('List', params.createIntChoiceConstraint(newConstraint));




    ogscript.debug(nodeList.getLength());</task>
    </button>
    <label height="134" id="data-display" left="119" style="txt-align:west" top="238" width="473"/>
    </abs>

    #DashBoard


  • 3.  RE: Dynamic dropdown from XML data

    Posted 06-19-2020 16:26

    James, this is truly fantastic. Thank you so much for not only providing a solution, but for also providing full working sample code that does exactly what I requested. I have already expanded on your code to get closer to my end solution. This will allow me to create a very elegant system of pre-checking a huge list of potential name keys to ensure they are correct.

    Code snippets like these are great resources to those of us trying to figure out how to accomplish things, and a great searchable resource! Much appreciated.

    Cheers, Dave

     


    #DashBoard