Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49002 > unrolled thread
| Started by | Jason Friedman <jsf80238@gmail.com> |
|---|---|
| First post | 2013-06-23 13:15 -0600 |
| Last post | 2013-06-23 13:15 -0600 |
| 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.
Re: Finding all instances of a string in an XML file Jason Friedman <jsf80238@gmail.com> - 2013-06-23 13:15 -0600
| From | Jason Friedman <jsf80238@gmail.com> |
|---|---|
| Date | 2013-06-23 13:15 -0600 |
| Subject | Re: Finding all instances of a string in an XML file |
| Message-ID | <mailman.3735.1372014910.3114.python-list@python.org> |
[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 top | Article view | comp.lang.python
csiph-web