xml problem in parsing the great grandchildren

Peter Otten __peter__ at web.de
Thu Nov 13 10:47:04 CET 2014


OmPs wrote:


> import xml.etree.ElementTree as ET
> 
> inv = open('lshw.xml', 'r')
> inv = inv.read()
> inv = ET.XML(inv)
> 
> find_memory =inventory.findall(".//node[@id='bank:*']")
> 
> # I am stuck here. What is required is description of the system memory,
> but system has lot of places where class=memory is used and hence i can't
> use it. i need the one's which has id=bank:0 .. n and collect the
> decryption of it.

 
> I am stuck here. What I am looking for  is description of the system
> memory, but system has lot of places where class=memory is used and hence
> i can't use it. i need the one's which has id=bank:0 .. n and collect the
> decryption of it.
> 
> I don't have option to use lxml.

Take the candidates and then find criteria the further reduce the matching 
nodes until only those are left that you are interested in:

import xml.etree.ElementTree as ET
 
inv = ET.parse("lshw-a105.xml")
 
nodes = inv.findall(".//node[@class='memory']")
for node in nodes:
    description = node.find("./description")
    if description.text == "System Memory":
        for subnode in node.findall("./node"):
            print(subnode.attrib["id"], subnode.find("./description").text)
        break





More information about the Python-list mailing list