Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88776 > unrolled thread

Re: Help with ElementTree

Started byLarry Martell <larry.martell@gmail.com>
First post2015-04-10 06:49 -0400
Last post2015-04-10 06:49 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Help with ElementTree Larry Martell <larry.martell@gmail.com> - 2015-04-10 06:49 -0400

#88776 — Re: Help with ElementTree

FromLarry Martell <larry.martell@gmail.com>
Date2015-04-10 06:49 -0400
SubjectRe: Help with ElementTree
Message-ID<mailman.200.1428662956.12925.python-list@python.org>
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. :-(

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web