Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #48844
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'tree': 0.05; '"""': 0.07; 'attribute': 0.07; 'subject:file': 0.07; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; '{},': 0.09; 'def': 0.12; '(),': 0.16; 'be:': 0.16; 'elem': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:XML': 0.16; 'wrote:': 0.18; 'small,': 0.19; 'appears': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'skip:{ 20': 0.24; 'looks': 0.24; 'header:X-Complaints-To:1': 0.27; 'xml': 0.29; 'lines': 0.31; 'file': 0.32; 'subject:all': 0.32; 'actual': 0.34; 'leads': 0.36; 'yield': 0.36; 'jason': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'times': 0.62; 'such': 0.63; '2000': 0.65; 'levels': 0.65 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Finding all instances of a string in an XML file |
| Date | Fri, 21 Jun 2013 08:16 +0200 |
| Organization | None |
| References | <CANy1k1igA3PLR-Pgy9w4T1pbn7d2tK9=6tNpnett6iTjACDs7w@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p50849db0.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| 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.3652.1371795314.3114.python-list@python.org> (permalink) |
| Lines | 64 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1371795314 news.xs4all.nl 16005 [2001:888:2000:d::a6]:39060 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:48844 |
Show key headers only | View raw
Jason Friedman wrote:
> I have XML which looks like:
>
> <?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>
>
> The "Property X" string appears twice times and I want to output the
> "path"
> that leads to all such appearances. In this case the output would be:
>
> LEVEL_1 {}, LEVEL_2 {"ATTR": "hello"}, ATTRIBUTE {"NAME": "Property X",
> "VALUE": "2"}
> LEVEL_1 {}, LEVEL_2 {"ATTR": "goodbye"}, LEVEL_3 {"ATTR": "aloha"},
> ATTRIBUTE {"NAME": "Property X", "VALUE": "3"}
>
> My actual XML file is 2000 lines and contains up to 8 levels of nesting.
That's still small, so
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))
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Finding all instances of a string in an XML file Peter Otten <__peter__@web.de> - 2013-06-21 08:16 +0200
csiph-web