XML Newbie needing some serious help..

Discussion in 'Python' started by rh0dium, May 20, 2005.

  1. rh0dium

    rh0dium Guest

    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>
    rh0dium, May 20, 2005
    #1
    1. Advertising

  2. Simon Brunning, May 20, 2005
    #2
    1. Advertising

  3. > 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.
    Adriano Ferreira, May 20, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. raecer

    newbie needing help

    raecer, Nov 19, 2004, in forum: C++
    Replies:
    3
    Views:
    318
    Phlip
    Nov 19, 2004
  2. =?Utf-8?B?R3V5IFRob3JudG9u?=

    Needing some help with Business Objects in ASP.Net PLEASE

    =?Utf-8?B?R3V5IFRob3JudG9u?=, Nov 9, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    329
    =?Utf-8?B?R3V5IFRob3JudG9u?=
    Nov 9, 2006
  3. Replies:
    4
    Views:
    985
    Russell
    Mar 2, 2007
  4. Hendrik Maryns

    Newbie needing some help with segmentation fault

    Hendrik Maryns, Dec 4, 2007, in forum: C Programming
    Replies:
    20
    Views:
    833
    Hendrik Maryns
    Dec 7, 2007
  5. Kelly Brady

    Serious Newbie that would like some help

    Kelly Brady, Oct 15, 2003, in forum: ASP .Net Mobile
    Replies:
    0
    Views:
    103
    Kelly Brady
    Oct 15, 2003
Loading...

Share This Page