Parsing XML - Newbie help
Fredrik Lundh
fredrik at pythonware.com
Wed May 25 10:07:02 EDT 2005
"rh0dium" wrote:
> Ok so up to here I am ok. I find ( If you want the full xml let me
> know) two blocks of system memory. It MUST be "System Memory" only.
> Now how do I get a list of all of the children "nodes" of this. They
> are named bank:N ( i.e bank:0, bank:1 etc [see below] ). For each one
> of those there may ( or may not ) have some memory stuck in it. I can
> tell if there is memory because a size is given. I want to a list of
> all of the sizes. From there I can say you have sum(memory) in
> len(memory) banks of total banks.
once you've found a memory node, you should be able to simply loop
over all node children, optionally filter on the id attribute, and look for
size elements.
sizes = []
for elem in tree.findall(".//node"):
if elem.get("class") == "memory":
if elem.findtext("description") == "System Memory":
print "Found system memory bank"
# loop over subnodes
for node in elem:
size = node.findtext("size")
if size:
sizes.append(int(size))
print len(sizes), sum(sizes)
note that this assumes that the size is always in bytes. if not,
you have to check the units attribute; e.g.
size_elem = node.find("size")
if size_elem is not None:
unit = size_elem.get("units", "bytes")
size = int(size_elem.text)
...
</F>
More information about the Python-list
mailing list