xml - Use python to determine what element(s) a value is in -
xml - Use python to determine what element(s) a value is in -
i've tried searching in every way can think, have had no luck. apologize in advance if has been asked answered in different way.
what need help accomplishing:
i have set of name values like: ['john smith', 'new york', 'toys'] , know exist in xml document, like:
<doc> <people> <name>john smith</name> </people> <places> <name>new york</name> </places> <things> <name>toys</name> </things> <about> <name>john smith male.</name> </about> </doc>
using elementtree, can loop through list , find values in document.
what i'm trying figure out how title states is:
loop through list , find values in document figure out xml tag(s) is/are around each value , homecoming tag namei can't figure out, imagine there must way accomplish without much heavy lifting. suggestions or recommendations appreciated.
if aren't tied elementtree, here simple illustration using lxml
(note: didn't loops you, can portion of work). tag contains text, parent of tag:
#!/usr/bin/env python3 lxml import etree lines = none open('ex.xml') f: lines = f.read() doc = etree.fromstring(lines) elem = doc.xpath("//name[text()='john smith']") e in elem: parent = e.getparent() print(parent.tag)
python xml
Comments
Post a Comment