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


Groups > comp.lang.python > #49002

Re: Finding all instances of a string in an XML file

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <jsf80238@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'tree': 0.05; '"""': 0.07; 'subject:file': 0.07; 'subject:string': 0.09; 'cc:addr:python- list': 0.11; 'def': 0.12; '(),': 0.16; 'elem': 0.16; 'subject:XML': 0.16; 'skip:= 10': 0.16; 'import': 0.22; 'cc:addr:python.org': 0.22; 'skip:e 30': 0.24; 'skip:{ 20': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'xml': 0.29; 'message-id:@mail.gmail.com': 0.30; 'skip:= 20': 0.31; 'subject:all': 0.32; 'received:google.com': 0.35; 'yield': 0.36; 'skip:& 10': 0.38; 'thank': 0.38; 'skip:& 20': 0.39; 'skip:n 10': 0.64; 'peter,': 0.84; 'to:none': 0.92
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:cc :content-type; bh=7Ptlxw3Hd50y+HWTFQIOV3z1yjUq4Y+58aiDpGU5ZAQ=; b=Jjx4a298x8tpY1mS9s5cORMxoLP0gdARDCt6sYCtUKkaxb2gYzCOinWR4W3L2wV2Ym 3BrTLk654E1cV5fh7iSgg5HayzRUh5SH9F9eiwuBu64EPunJSlEIb1GXGZZlfrilQkYh TCiMzi0koEzN47Lgt8dquN+RxTxfhVyWOl31W254TYkgBvNLDPMYYA7slmb/p+/AIgV7 dwwI798D1PnKFGySqITpkivfTjLKOrU9f1IdckscU4j2TsnZOsDmkHQLA2e6e5Nfryk4 IGNduiZq9Op4nGk2TTQWF15gdr3jjpxJxzkIsezZMKQxN2k1uSqcDQMqWjf2RkvL+OhR fasQ==
MIME-Version 1.0
X-Received by 10.50.129.68 with SMTP id nu4mr3967608igb.9.1372014901612; Sun, 23 Jun 2013 12:15:01 -0700 (PDT)
In-Reply-To <kq0r0m$bn$1@ger.gmane.org>
References <CANy1k1igA3PLR-Pgy9w4T1pbn7d2tK9=6tNpnett6iTjACDs7w@mail.gmail.com> <kq0r0m$bn$1@ger.gmane.org>
Date Sun, 23 Jun 2013 13:15:01 -0600
Subject Re: Finding all instances of a string in an XML file
From Jason Friedman <jsf80238@gmail.com>
Cc python-list <python-list@python.org>
Content-Type multipart/alternative; boundary=047d7b414030d6641904dfd71cf1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3735.1372014910.3114.python-list@python.org> (permalink)
Lines 87
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1372014910 news.xs4all.nl 15864 [2001:888:2000:d::a6]:34193
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49002

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

> xml = """<?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE KMART SYSTEM "my.dtd">
> <LEVEL_1>
>   <LEVEL_2 ATTR="hello">
>     <ATTRIBUTE NAME="Property X" VALUE ="2"/>
>   </LEVEL_2>
>   <LEVEL_2 ATTR="goodbye">
>     <ATTRIBUTE NAME="Property Y" VALUE ="NULL"/>
>     <LEVEL_3 ATTR="aloha">
>       <ATTRIBUTE NAME="Property X" VALUE ="3"/>
>     </LEVEL_3>
>     <ATTRIBUTE NAME="Property Z" VALUE ="welcome"/>
>   </LEVEL_2>
> </LEVEL_1>
> """
>
> import xml.etree.ElementTree as etree
>
> tree = etree.fromstring(xml)
>
> def walk(elem, path, token):
>     path += (elem,)
>     if token in elem.attrib.values():
>         yield path
>     for child in elem.getchildren():
>         for match in walk(child, path, token):
>             yield match
>
> for path in walk(tree, (), "Property X"):
>     print(", ".join("{} {}".format(elem.tag, elem.attrib) for elem in
> path))
>
> Peter, thank you, that exactly meets my need.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Finding all instances of a string in an XML file Jason Friedman <jsf80238@gmail.com> - 2013-06-23 13:15 -0600

csiph-web