Facility Control

 View Only
Expand all | Collapse all

Parsing an XML file and using the Elements in DashBoard

  • 1.  Parsing an XML file and using the Elements in DashBoard

    Posted 08-07-2019 00:00

    I am moving onto the next phase in my project. I want to parse an XML file and use the elements in it to populate a graphic in Dashboard.

     

    I have mixed and matched code from all over to attempt to accomplish this but no luck.

     

    Can nyone spot an obvious error in the cryptic code ? Or suggest another approach?

     

    var filepath = ogscript.getPrivateString('constants','filepath');

    var XMLDoc;

    ogscript.debug('Reading XML file: ' );

    XMLDoc = ogscript.parseXML('file:/H:/CCHL2019/Sportzcast/ScoreBoardHD2.xml'); // this line reads in the XML file, XMLDoc is set null on failure

    if (XMLDoc == null) {

    ogscript.debug('Failed to read file');

    } else {

    ogscript.debug('Parsing XML');

    var found=XMLDoc.getElementsByTagName('info'); // this line creates a nodeList

    ogscript.debug('search found ' + found.length + ' items');

     

     

     

    for (var i = 0; i < found.length; i++) { // for each item in the node list...

    var node = found.item(i);
    var first = node.getAttribute('site');
    var name = children.item(i).firstChild.nodeValue // and the next will have the name data.

    // ogscript.debug(i + ": " + name + ", " + first);


    //params.setValue(teamList, i, name);

    // params.setValue(0x3, i, Number(num));

    }
    }

     

    Thanks

     

    Whit



  • 2.  RE: Parsing an XML file and using the Elements in DashBoard

    Posted 08-08-2019 01:08

    After snooping around the community I found a couple of examples, that worked perfectly for me, using their sample XML file. BUT... when I tried using the actual file I want to parse and grab the elements from, the coding failed.

     

    This file worked:

     

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <teams>
    <team ID="1" name="Red Team"/>
    <team ID="2" name="Blue Team"/>
    <team ID="3" name="Green Team"/>
    <team ID="3" name="Your Team"/>
    </teams>

     

    But the file I want to use is constructed differently and I couldn't extract the elements:

    <?xml version="1.0" encoding="utf-8"?>
    <info>
    <site>*Internal Test BOT</site>
    <BOT>0</BOT>
    <layer>
    </layer>
    <gameid>
    </gameid>
    <vendor>Sportzcast</vendor>
    <sport>Football</sport>
    <clock> 5:35</clock>
    <clockmin> 5</clockmin>
    <clocksec>35</clocksec>
    <playclock>23</playclock>
    <clockstatus>Running</clockstatus>
    <clockmode>:</clockmode>
    <playclockstatus>
    </playclockstatus>
    <horn>
    </horn>
    <Hscore>66</Hscore>
    <Vscore>48</Vscore>

    </info>

    This file has the root element <info> and the child elements are what I want to extract and use.

     

    What coding would I use to extract items like clock, Hscore>etc

     

    Thanks

     

    Whit

     

     


    #DashBoard


  • 3.  RE: Parsing an XML file and using the Elements in DashBoard

    Posted 08-08-2019 20:46

    Hi Whit.

    One way to extract relevant nodes is to use XPath:

    https://www.w3schools.com/xml/xpath_intro.asp

     

    You can also get getChildren() or getElementsByTagName on any node in your document.

    In the end, you'll also need to use getTextContent() on your nodes instead of getAttribute("ATTRIB_NAME")


    #DashBoard


  • 4.  RE: Parsing an XML file and using the Elements in DashBoard

    Posted 08-09-2019 15:18

    Thanks James

     

    I have it working !! My solution is not elegant and it is best defined as circuitous ! James I did heed your advice about using getTextContent and that was a key.

     

    I have been an irritation folks with my posting of code in the body of a post. What is the proper method of posting code with the scroll bars etc, here in this community?

     

    I would like to post my code and ask about a more elegant solution.

     

    Thanks ever so much

     

    Whit

     

    I now have a handle on 2/3 of my project. One aspect to go.


    #DashBoard


  • 5.  RE: Parsing an XML file and using the Elements in DashBoard

    Posted 08-09-2019 17:47

    There is a drop down option to select "code" to have it format with scroll bars. It still removes leading spaces (for now) but is a little better for proper code viewing.


    #DashBoard


  • 6.  RE: Parsing an XML file and using the Elements in DashBoard

    Posted 08-09-2019 18:56

    Thank you sir.

     

    Whit


    #DashBoard


  • 7.  RE: Parsing an XML file and using the Elements in DashBoard

    Posted 08-09-2019 19:16

    Okay, here is the circuitous and non elegant code for parsing my XML file and assigning the values to "labels' in Dashboard. There must be a smoother technque for doing this..

    I used the root element //* which allowed me access to all 52 elements in the XML file

    var XMLDoc;

    ogscript.debug('Reading XML file: ' );

    XMLDoc = ogscript.parseXML('file:/H:/CCHL2019/Sportzcast/ScoreBoardHD2.xml'); // this line reads in the XML file, XMLDoc is set null on failure

    if (XMLDoc == null) {

    ogscript.debug('Failed to read file');

    } else {

    ogscript.debug('Parsing XML');


    var found = ogscript.runXPath('//*',XMLDoc);
    ogscript.debug('search found ' + found.length + ' items');


    for (var i = 0; i < found.length; i++) { // for each item in the node list...
    //var i=0;
    var node = found.item(i);
    var site = node.getTextContent();
    // ogscript.debug(node);
    if (i==7) {params.setValue(0x44,0, site); }
    if (i==15) {params.setValue(0x45,0, site); }
    if (i==16) {params.setValue(0x46,0, site); }

    }
    }

    #DashBoard


  • 8.  RE: Parsing an XML file and using the Elements in DashBoard

    Posted 09-21-2022 12:00
    Hi Whit!

    Did you ever find a more streamlined way to do this? I'm currently working on building the same thing with a scorebot and need some guidence!

    Ryan

    ------------------------------
    Ryan Corcoran
    Freelancer TV Professional
    RCTV
    ------------------------------



  • 9.  RE: Parsing an XML file and using the Elements in DashBoard

    Posted 09-21-2022 15:36
    Hi Ryan
    If you have an example of the XML you are trying to parse and can indicate which tag or attribute you care about, we may be able to recommend an approach.


    ------------------------------
    James Peltzer
    Ross Video
    ------------------------------



  • 10.  RE: Parsing an XML file and using the Elements in DashBoard

    Posted 09-22-2022 13:06
    Hi James!

    I was actually able to make Whit's code work with some modifications - so everything is good, though I'd still be happy to learn if there is a more efficient way to address this these days. Below is my Dashboard panel - the lower portion between the "XML File Path" param and the "Check Data" button is what I am extracting from the XML file that Sportzcast's ScoreBot is writing to. 


    Here is script that is running on the "Check Data" button:

    Finally, here is the XML format:

    Thanks!

    Ryan

    ------------------------------
    Ryan Corcoran
    Freelancer TV Professional
    RCTV
    ------------------------------



  • 11.  RE: Parsing an XML file and using the Elements in DashBoard

    Posted 09-28-2022 06:35

    Hi, Ryan.

    Could you attach GRID file, please? 

    Thanks. 



    ------------------------------
    Shamil Gasim
    TV and Radio Engineer
    ------------------------------