Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #88776
| References | <CACwCsY4-3_7RBtx2LTLZ6pEAk-qW31XxeZz4aFVCmyK6f4Tong@mail.gmail.com> <mg6h07$uh$1@ger.gmane.org> <CACwCsY63BwubfhmNydzsu8Pg5qSKt6MJ_-N44hh0+yBSmZM7Sg@mail.gmail.com> |
|---|---|
| Date | 2015-04-10 06:49 -0400 |
| Subject | Re: Help with ElementTree |
| From | Larry Martell <larry.martell@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.200.1428662956.12925.python-list@python.org> (permalink) |
On Thu, Apr 9, 2015 at 2:50 PM, Larry Martell <larry.martell@gmail.com> wrote:
> On Thu, Apr 9, 2015 at 2:39 PM, Peter Otten <__peter__@web.de> wrote:
>> Larry Martell wrote:
>>
>>> I have an XML file that looks like this (this is just the pertinent
>>> part, the file is huge):
>>>
>>> <?xml version="1.0"?>
>>> <Root>
>>> <Doc Type="CCI">
>>> <Node Name="SystemConfig" Section="yes">
>>> <Node Name="Environment">
>>> <Parameter Name="ToolName">
>>> <Value>
>>> <Default>KA21</Default>
>>> <Current>KA21</Current>
>>> </Value>
>>> </Parameter>
>>> </Node>
>>> </Node>
>>>
>>> <Node Name="Events" Section="yes">
>>> <Parameter Name="LastEventExportTime">
>>> <Value>
>>> <Default>00:00:00</Default>
>>> <Current>15/03/2014 05:56:00</Current>
>>> </Value>
>>> </Parameter>
>>> </Node>
>>> </Doc>
>>> </Root>
>>>
>>> I would like to use ElementTree to get 2 values from this:
>>>
>>> SystemConfig.Environment.ToolName.Current
>>> Events.LastEventExportTime.Current
>>>
>>> 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?
>>
>> Try it in the interactive interpreter, one step after another:
>>
>>>>> from xml.etree import ElementTree
>>>>> root = ElementTree.fromstring("""<?xml version="1.0"?>
>> ... <Root>
>> <snip>
>> ... </Root>
>> ... """)
>>>>> root.find("Doc")
>> <Element 'Doc' at 0x7f12af916098>
>>>>> root.find("Doc/Node")
>> <Element 'Node' at 0x7f12af9218b8>
>>>>> root.find("Doc/Node/Node/Parameter/Value/Current").text
>> 'KA21'
>>
>> That "worked" because the Node elements involved are the first in the
>> document. You may have to add more "conditions" until there is no ambiguity
>> left, e. g. to pick the child node of Doc with the Name="Events" attribute:
>>
>>>>> root.find("Doc/Node[@Name='Events']/Parameter/Value/Current").text
>> '15/03/2014 05:56:00'
>>
>> See
>>
>> https://docs.python.org/dev/library/xml.etree.elementtree.html#xpath-support
>>
>> for the details.
>
> Thanks! I did try in the interpreter and I did look at the doc you
> referenced. I just couldn't get it to do what I wanted. I understand
> this a lot better now. My example XML was grossly simplified, but I
> can get what I need with this syntax:
>
> root.find("Doc/Node[@Name='Events']/Parameter[@Name='LastEventExportTime']/Value/Current").text
> root.find("Doc/Node[@Name='SystemConfig']/Node[@Name='Environment']/Parameter[@Name='ToolName']/Value/Current").text
So I tested this on a machine running 2.7, but then when I deployed it
to my client's machine it did not work. Turns out they're running 2.6
which I find does not support searching for attributes using the
[@attribute] syntax. They do not want to upgrade, so I have to find a
way to do this without using that. :-(
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Help with ElementTree Larry Martell <larry.martell@gmail.com> - 2015-04-10 06:49 -0400
csiph-web