Parsing an XML file and using the Elements in DashBoard

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

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"?>

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

<?xml version="1.0" encoding="utf-8"?> *Internal Test BOT 0 Sportzcast Football 5:35 5 35 23 Running : 66 48

This file has the root element 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

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

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

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

Thank you sir.

Whit

#DashBoard

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

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

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.

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

Hi, Ryan.

Could you attach GRID file, please?

Thanks.