Help with ElementTree
ben+python at benfinney.id.au
Thu Apr 9 20:42:50 CEST 2015
Larry Martell <larry.martell at gmail.com> writes:
> I have an XML file that looks like this (this is just the pertinent
> part, the file is huge):
It's also not a very helpful schema. Elements called “Node”, where the
actual type of element is in an attribute, just make your job needlessly
They also pointlessly force you to use mixed case, when the convention
is to name all the elements in lowercase (‘parameter’, ‘current’, etc.).
That said: What you need for this kind of searching within XML documents
is the XPath query language. ElementTree has limited support for it
enough for your stated requirements.
> I would like to use ElementTree to get 2 values from this:
>>> from xml.etree import ElementTree
>>> doc_text = """… your example document text here …"""
>>> doc_tree = ElementTree.fromstring(doc_text)
>>> toolname_xpath = ".//Node[@Name='SystemConfig']/Node[@Name='Environment']/Parameter[@Name='ToolName']//Current"
>>> toolname_element = doc_tree.find(toolname_xpath)
<Element 'Current' at 0x7f3655452c28>
>>> lasteventexporttime_xpath = ".//Node[@Name='Events']/Parameter[@Name='LastEventExportTime']//Current"
>>> lasteventexporttime_element =
<Element 'Current' at 0x7f3655452db8>
> I've been trying for hours to get ElementTree to give me these 2
> values, but nothing I do seems to work. Can anyone help me with this?
Beat the designers of that document upside the head until they give you
a more easily-parsed schema.
\ “Considering the current sad state of our computer programs, |
`\ software development is clearly still a black art, and cannot |
_o__) yet be called an engineering discipline.” —Bill Clinton |
More information about the Python-list