Parser XML in python -


I have some database like another IM in XML trying to parse it with Python 2.7:

  & lt; Team & gt; & Lt; Generator & gt; & Lt; TEAM_NAME & gt; TeamMaster & lt; / TEAM_NAME & gt; & Lt; Team_year & gt; 2000 & lt; / team_year & gt; & Lt; Team_city & gt; NewYork & lt; / Team_city & gt; & Lt; / Generator & gt; & Lt; Players & gt; & Lt; Definition name = "John V." Number = "4" age = "25" & gt; & Lt; Criterion Status = "Fove" side = "True" & gt; & Lt; Criterion website = "www.johnV.com" version = "1" result = "true" /> & Lt; / Criteria & gt; & Lt; Object debit = "2003" version = "3" flag = "full" & gt; & Lt; History item_ref = "team34" /> & Lt; History item_ref = "predominance" /> & Lt; / Definition & gt; & Lt; Definition Name = "Emma" number = "2" age = "19" & gt; & Lt; Criterion Status = "middle" side = "left" & gt; & Lt; Criterion website = "www.emma.net" version = "7" result = "true" /> & Lt; / Criteria & gt; & Lt; Object debit = "2008" version = "1" flag = "full" & gt; & Lt; History item_ref = "newteam" /> & Lt; History item_ref = "Youth" /> & Lt; History item_ref = "oldteam" /> & Lt; / Definition & gt; & Lt; / Players & gt; & Lt; / Team & gt;   

With this short script, I can easily parse the part "generator" before my XML, where I know that there are all the elements in it:

  xml to Dom.minidom import parseString mydb = {"team_name": "team_year": "team_data":} file = open ('mydb.xml', 'r') data = file.read () File.close () dom = ParseString (data) # Retrieve the XML tag (& lt; tag & gt; data & lt; / tag & gt;), which is found with the parser name tagname: xmlTag = dom.getElementsByTagName ('team_name') [0] .toxml () #strip off ('& lt; team_name & gt;', 'mydb ["team_name"] = xmlData # TeamMaster   

But my real problem came when I tried to parse "players" elements, where the attributes "definition" and an unknown number of element in history. "Maybe there is another module that minimizes me ?

Better use xml.etree element, it has a more Python syntax < Get the text of team_name by Code> root.findtext ('team_name') or Repeat on all definitions with root.finditer ('Definitions') .

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -