Help with ElementTree

Larry Martell larry.martell at gmail.com
Thu Apr 9 20:54:14 CEST 2015


On Thu, Apr 9, 2015 at 2:42 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
> 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
> harder.
>
> 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
> <URL:https://docs.python.org/3/library/xml.etree.elementtree.html#elementtree-xpath>,
> 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)
>
>> SystemConfig.Environment.ToolName.Current
>
>     >>> toolname_xpath = ".//Node[@Name='SystemConfig']/Node[@Name='Environment']/Parameter[@Name='ToolName']//Current"
>     >>> toolname_element = doc_tree.find(toolname_xpath)
>     >>> toolname_element
>     <Element 'Current' at 0x7f3655452c28>
>     >>> toolname_element.text
>     'KA21'
>
>> Events.LastEventExportTime.Current
>
>     >>> lasteventexporttime_xpath = ".//Node[@Name='Events']/Parameter[@Name='LastEventExportTime']//Current"
>     >>> lasteventexporttime_element =
>     >>> doc_tree.find(lasteventexporttime_xpath)
>     >>> lasteventexporttime_element
>     <Element 'Current' at 0x7f3655452db8>
>     >>> lasteventexporttime_element.text
>     '15/03/2014 05:56:00'


Thanks. I have this working with the help of Peter's reply.

>> 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.

It's programmatically generated by various pieces of semiconductor
manufacturing equipment. It's not in my power to get that changed.



More information about the Python-list mailing list