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


Groups > comp.lang.python > #88776

Re: Help with ElementTree

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <larry.martell@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interpreter': 0.05; 'root': 0.05; 'deployed': 0.07; 'ambiguity': 0.09; 'attributes': 0.09; "client's": 0.09; 'part,': 0.09; 'subject:Help': 0.11; '""")': 0.16; '00:00:00': 0.16; ':-(': 0.16; 'attribute:': 0.16; 'interpreter,': 0.16; 'left,': 0.16; 'skip:r 90': 0.16; 'wanted.': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'thu,': 0.19; 'seems': 0.21; 'machine': 0.22; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'this?': 0.23; 'skip:e 30': 0.24; 'url:dev': 0.24; 'looks': 0.24; "i've": 0.25; 'this:': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; '(this': 0.29; 'xml': 0.29; 'document.': 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'that.': 0.31; 'doc': 0.31; 'larry': 0.31; 'node': 0.31; 'skip:r 60': 0.31; 'anyone': 0.31; 'file': 0.32; 'thanks!': 0.32; 'url:python': 0.33; 'running': 0.33; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; '2.6': 0.36; 'url:org': 0.36; 'searching': 0.37; 'step': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; "couldn't": 0.39; 'to:addr:python.org': 0.39; 'details.': 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'pick': 0.64; 'more': 0.64; 'hours': 0.66; '2015': 0.84; 'otten': 0.84; 'upgrade,': 0.84; 'received:mail-wi0-x229.google.com': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=Ba1HC/Aot5wWNk60GYPSqyYKcVMlneGXJLsyGuJShhg=; b=ZmSU0Ejs8qqshrgBHLfWdtqhedsHtkaJJ0HP4YiCjP50o3cyaYySr7sp981iDMWeoe dvOgg12FZCAj3t22k9eMzZILe8PPvfcVJHoop4RWrfHqMJnfun9CvklBVitN3PE8/1hI SaGTAtPYhO5geEcafkAs1nh0YGF3zrHRD2O1hkDQsIIXN+/EEq5rpVODoScDCipdcZud GUKwTRoLLPCA55TYia9TJ799UgVJ2NWvRWiTQ18YEa7HbSQ2xE3Pd+s39V5Hexo2qJhk hjlW2vqDOszuAbY4XzzbCJdyvXf27rAUmz1F/LB00wOvhlUsY6ar/pPWQ1owp+ZWQNO9 hkoQ==
MIME-Version 1.0
X-Received by 10.180.218.195 with SMTP id pi3mr3807691wic.71.1428662947947; Fri, 10 Apr 2015 03:49:07 -0700 (PDT)
In-Reply-To <CACwCsY63BwubfhmNydzsu8Pg5qSKt6MJ_-N44hh0+yBSmZM7Sg@mail.gmail.com>
References <CACwCsY4-3_7RBtx2LTLZ6pEAk-qW31XxeZz4aFVCmyK6f4Tong@mail.gmail.com> <mg6h07$uh$1@ger.gmane.org> <CACwCsY63BwubfhmNydzsu8Pg5qSKt6MJ_-N44hh0+yBSmZM7Sg@mail.gmail.com>
Date Fri, 10 Apr 2015 06:49:07 -0400
Subject Re: Help with ElementTree
From Larry Martell <larry.martell@gmail.com>
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.200.1428662956.12925.python-list@python.org> (permalink)
Lines 82
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1428662956 news.xs4all.nl 2895 [2001:888:2000:d::a6]:59624
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:88776

Show key headers only | View raw


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


Thread

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

csiph-web