XML Newbie needing some serious help..

R

rh0dium

HI all,

I am looking to parse a unix tool called lshw (
http://ezix.sourceforge.net/software/lshw.html ). Now this provides a
nice XML output which looks similar to the bottom of this message..

Now I want to parse and get some information from it so here is what I
have..

class HWParser:
def __init__(self):
if os.path.isfile(LSHW):
lshw=Exec.Exec(LSHW)
lshw.execute('-xml')
else:
print "lshw does not exist", LSWH
return "Unknown"
self.data = lshw.read()
self.error = lshw.error()
self.exit = lshw.poll()

if self.error:
print "Error Exist", self.error
return
else:
self.xml = minidom.parseString(self.data)
self.xml.normalize()
self.context=xml.xpath.Context.Context(self.xml)

def CPUType(self):
nodes = xml.xpath.Evaluate("//processor/description/text()",
context=self.context)
for node in nodes:
print node.nodeValue

def p (self):
print self.xml.toxml()

c=HWParser()
c.p()
c.CPUType()

Now if you can't tell I am a newbie and really want to have some help
here. I know the c.p does work and it does spit out all of the XML
backout. So getting the data in is not a problem, getting it out
appears to be a problem.

Basically my problem is that I want to bet able to say get me the type
of CPU we have. I know I could grep this information but I'd rather do
it the proper way.
So when I call c.CPUType it should return
[ Intel(R) Xeon(TM) CPU 2.8Ghz, Intel(R) Xeon(TM) CPU 2.8Ghz ]
because there is two of them..

Am I asking too much? Probably just an idiot at the wheel :) Feel
free to comment on my style as well as hey - that's probably a bit
messy too :)


Thanks much


--------------------------------------------------



<?xml version="1.0" standalone="yes" ?>
<!-- generated by lshw-B.02.03 -->
<!-- GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-42) -->
<node id="cpu:0" claimed="true" class="processor"
handle="DMI:0400">
<description>CPU</description>
<product>Intel(R) Xeon(TM) CPU 2.80GHz</product>
<vendor>Intel Corp.</vendor>
<physid>400</physid>
<businfo>cpu@0</businfo>
<version>15.2.7</version>
<slot>Proc_1</slot>
<size units="Hz">2800000000</size>
<capacity units="Hz">3000000000</capacity>
<width units="bits">32</width>
<clock units="Hz">400000000</clock>
<configuration>
<setting id="id" value="6" />
</configuration>
<capabilities>
<capability id="fpu" >mathematical co-processor</capability>
<capability id="fpu_exception" >FPU exceptions
reporting</capability>
<capability id="wp" />
<capability id="vme" >virtual mode extensions</capability>
<capability id="de" >debugging extensions</capability>
<capability id="pse" >page size extensions</capability>
<capability id="tsc" >time stamp counter</capability>
<capability id="msr" >model-specific registers</capability>
<capability id="pae" >4GB+ memory addressing (Physical
Address Extension)</capability>
<capability id="mce" >machine check exceptions</capability>
<capability id="cx8" >compare and exchange
8-byte</capability>
<capability id="apic" >on-chip advanced programmable
interrupt controller (APIC)</capability>
<capability id="sep" >fast system calls</capability>
<capability id="mtrr" >memory type range
registers</capability>
<capability id="pge" >page global enable</capability>
<capability id="mca" >machine check architecture</capability>
<capability id="cmov" >conditional move
instruction</capability>
<capability id="pat" >page attribute table</capability>
<capability id="pse36" >36-bit page size
extensions</capability>
<capability id="clflush" />
<capability id="dts" >debug trace and EMON store
MSRs</capability>
<capability id="acpi" >thermal control (ACPI)</capability>
<capability id="mmx" >multimedia extensions
(MMX)</capability>
<capability id="fxsr" >fast floating point
save/restore</capability>
<capability id="sse" >streaming SIMD extensions
(SSE)</capability>
<capability id="sse2" >streaming SIMD extensions
(SSE2)</capability>
<capability id="ss" >self-snoop</capability>
<capability id="ht" >HyperThreading</capability>
<capability id="tm" >thermal interrupt and
status</capability>
</capabilities>
<node id="logicalcpu:0" claimed="true" class="processor"
handle="CPU:6.0">
<description>Logical CPU</description>
<physid>6.1</physid>
<width units="bits">32</width>
<capabilities>
<capability id="logical" >Logical CPU</capability>
</capabilities>
</node>
<node id="logicalcpu:1" claimed="true" class="processor"
handle="CPU:6.1">
<description>Logical CPU</description>
<physid>6.2</physid>
<width units="bits">32</width>
<capabilities>
<capability id="logical" >Logical CPU</capability>
</capabilities>
</node>
</node>
<node id="cpu:1" disabled="true" claimed="true" class="processor"
handle="DMI:0401">
<description>CPU</description>
<product>Intel(R) Xeon(TM) CPU 2.80GHz</product>
<vendor>Intel Corp.</vendor>
<physid>401</physid>
<businfo>cpu@1</businfo>
<version>15.2.7</version>
<slot>Proc_2</slot>
<size units="Hz">2800000000</size>
<capacity units="Hz">3000000000</capacity>
<width units="bits">32</width>
<clock units="Hz">400000000</clock>
<configuration>
<setting id="id" value="6" />
</configuration>
<capabilities>
<capability id="fpu" >mathematical co-processor</capability>
<capability id="fpu_exception" >FPU exceptions
reporting</capability>
<capability id="wp" />
<capability id="vme" >virtual mode extensions</capability>
<capability id="de" >debugging extensions</capability>
<capability id="pse" >page size extensions</capability>
<capability id="tsc" >time stamp counter</capability>
<capability id="msr" >model-specific registers</capability>
<capability id="pae" >4GB+ memory addressing (Physical
Address Extension)</capability>
<capability id="mce" >machine check exceptions</capability>
<capability id="cx8" >compare and exchange
8-byte</capability>
<capability id="apic" >on-chip advanced programmable
interrupt controller (APIC)</capability>
<capability id="sep" >fast system calls</capability>
<capability id="mtrr" >memory type range
registers</capability>
<capability id="pge" >page global enable</capability>
<capability id="mca" >machine check architecture</capability>
<capability id="cmov" >conditional move
instruction</capability>
<capability id="pat" >page attribute table</capability>
<capability id="pse36" >36-bit page size
extensions</capability>
<capability id="clflush" />
<capability id="dts" >debug trace and EMON store
MSRs</capability>
<capability id="acpi" >thermal control (ACPI)</capability>
<capability id="mmx" >multimedia extensions
(MMX)</capability>
<capability id="fxsr" >fast floating point
save/restore</capability>
<capability id="sse" >streaming SIMD extensions
(SSE)</capability>
<capability id="sse2" >streaming SIMD extensions
(SSE2)</capability>
<capability id="ss" >self-snoop</capability>
<capability id="ht" >HyperThreading</capability>
<capability id="tm" >thermal interrupt and
status</capability>
</capabilities>
<node id="logicalcpu:0" claimed="true" class="processor"
handle="CPU:6.0">
<description>Logical CPU</description>
<physid>6.1</physid>
<width units="bits">32</width>
<capabilities>
<capability id="logical" >Logical CPU</capability>
</capabilities>
</node>
<node id="logicalcpu:1" claimed="true" class="processor"
handle="CPU:6.1">
<description>Logical CPU</description>
<physid>6.2</physid>
<width units="bits">32</width>
<capabilities>
<capability id="logical" >Logical CPU</capability>
</capabilities>
</node>
</node>
 
A

Adriano Ferreira

That doesn't appear to be well-formed XML, which isn't a good start...

Indeed. rh0dium, you can't have two <node>s elements at root level.

If you use an enclosing element around the two <node>s, your XML
becomes well formed. Like this:

<?xml version="1.0" standalone="yes" ?>
<root>
<!-- generated by lshw-B.02.03 -->
<!-- GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-42) -->
<node id="cpu:0" claimed="true" class="processor" handle="DMI:0400">
...
</node>
<node id="cpu:1" disabled="true" claimed="true" class="processor"
handle="DMI:0401">
...
</node>
</root>

Regards, Adriano.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top