I've got the following function which allows me to search through json and return the value of the key that is searched for.
However, I'm trying to find the actual parentNode of the key being searched for and finding this a bit difficult.
def bar(somejson, key):
def val(node):
# Searches for the next Element Node containing Value
e = node.nextSibling
while e and e.nodeType != e.ELEMENT_NODE:
e = e.nextSibling
return (e.getElementsByTagName('string')[0].firstChild.nodeValue if e
else None)
# parse the JSON as XML
foo_dom = parseString(xmlrpclib.dumps((json.loads(somejson),)))
# and then search all the name tags which are P1's
# and use the val user function to get the value
return [val(node) for node in foo_dom.getElementsByTagName('name')
if node.firstChild.nodeValue in key]
Ive tried using parentNode instead of firstChild, but its not working. Any idea where I'm going wrong?