1

Attempting to create a python script to display data from a complex XML file that changes every 5-7 seconds. Below is a sample XML file that I'm working with...

<bsgame source="TAS Baseball/Softball" version="5.17.00" 
generated="4/9/2019">
<venue gameid="SB0409-1"
       visid="IUP-S" homeid="JOHN" visname="Indiana (PA)" 
homename="Seton Hill"
       date="4/9/2019" location="Greensburg, PA" 
stadium="" duration="" attend="0"
       leaguegame="N" start="3:00 PM" dhgame="1" 
schedinn="7"
       weather="sunny and warm">
<umpires></umpires>
<notes>
  <note text="Weather: sunny and warm"></note>
  <note text="Game: livegame"></note>
</notes>
<rules batters="9,9" usedh="Y"></rules>
</venue>
<team vh="V" id="IUP-S" name="Indiana (PA)" code="IUP-S">
<linescore line="0,0,0,0,1,1,3" runs="5" hits="9" 
 errs="2" lob="7">
  <lineinn inn="1" score="0"></lineinn>
  <lineinn inn="2" score="0"></lineinn>
  <lineinn inn="3" score="0"></lineinn>
  <lineinn inn="4" score="0"></lineinn>
  <lineinn inn="5" score="1"></lineinn>
  <lineinn inn="6" score="1"></lineinn>
  <lineinn inn="7" score="3"></lineinn>
</linescore>
<team vh="H" id="SHU-S" name="Seton Hill" code="SHU-S">
<linescore line="0,0,0,0,0,1,X" runs="1" hits="4" 
  errs="2" lob="3">
  <lineinn inn="1" score="0"></lineinn>
  <lineinn inn="2" score="0"></lineinn>
  <lineinn inn="3" score="0"></lineinn>
  <lineinn inn="4" score="0"></lineinn>
  <lineinn inn="5" score="0"></lineinn>
  <lineinn inn="6" score="1"></lineinn>
  <lineinn inn="7" score=""></lineinn>
</linescore>

I've noticed that services such as StretchInternet convert XML's like this to Json in order to display the data. I've figured out how to convert the XML to Json using xmltodict in Python.

 import json
 import xmltodict

 with open("/Volumes/GoogleDrive/My Drive/LIVE/XMLs/SB_Demo.xml", 'r') as f:
     xmlString = f.read()

 jsonString = json.dumps(xmltodict.parse(xmlString), indent=4)

 print("\nJSON output(output.json):")
 print(jsonString)

 with open("output.json", 'w') as f:
     f.write(jsonString)

I'm stumped on where to go from here. Eventually, I want to have the ability to show the total RUNS (@runs) from both "teams" on a video broadcast. BUT, I'm a step-by-step kind of guy. Step 1...how do I parse this XML using xmltodict and display just @runs for both "teams"?

Matt M
  • 691
  • 2
  • 6
  • 17

1 Answers1

0

For starters, you should stay consistent with your quotation usage (use " or ')

Also, your xml is missing the </team> closing tag after each <linescore>

As for your question - you are converting your xml dict into a JSON string with json.dumps, which won't be useful for what you're trying. Access your xmltodict object as a Python dict

with open("YOUR_XML_FILE.xml", 'r') as f:
    xmlString = f.read()

xmlDict = xmltodict.parse(xmlString)

for team in xmlDict['bsgame']['team']:
    print(team['@name'], 'Runs :')
    print(team['linescore']['@runs'])

Which results in

Indiana (PA) Runs :
5
Seton Hill Runs :
1
Matt M
  • 691
  • 2
  • 6
  • 17