#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0

    Error while reading xml file


    Hi friends,

    I am trying to read xml file using python.
    My python code is as follow
    Code:
    #!/usr/bin/python  from xml.dom import minidom  xmldoc=minidom.parse("RR.fz") module=xmldoc.getElementsByTagName("module")[0] instances=module.getElementsByTagName("instances")[0] instance=instances.getElementsByTagName("instance")  for inst in instance:         title=inst.getElementsByTagName("title")[0].firstChild.data         print(title)         views=inst.getElementsByTagName("views")[0]         #print(views)         schview=views.getElementsByTagName("schematicView")[0]         #print(schview)         connectors=schview.getElementsByTagName("connectors")[0]         #print(connectors)         connector=connectors.getElementsByTagName("connector")         print(connector)
    My sample xml file is as follow
    <?xml version="1.0" encoding="UTF-8"?> <module fritzingVersion="0.8.0b.06.12.08d5"> <views> <view name="breadboardView" backgroundColor="#cccccc" gridSize="0.1in" showGrid="0" alignToGrid="0" viewFromBelow="0"/> <view name="schematicView" backgroundColor="#f2f2f2" gridSize="0.295276in" showGrid="1" alignToGrid="1" viewFromBelow="0"/> <view name="pcbView" backgroundColor="#a0a8b3" gridSize="0.05in" showGrid="1" alignToGrid="1" viewFromBelow="0" autorouteTraceWidth="24"/> </views> <instances> <instance moduleIdRef="TwoLayerRectanglePCBModuleID" modelIndex="1768310" path=":/resources/parts/core/rectangle_pcb_two_layers.fzp"> <property name="layers" value="2"/> <property name="width" value="169.333333333333"/> <property name="height" value="56.4444444444444"/> <title>PCB1</title> <views> <pcbView layer="board"> <geometry z="1.5" x="-250" y="263"/> </pcbView> <breadboardView layer=""> <geometry z="0" x="0" y="0"/> </breadboardView> <schematicView layer=""> <geometry z="0" x="0" y="0"/> </schematicView> </views> </instance> <instance moduleIdRef="Breadboard-RSR03MB102-ModuleID" modelIndex="1768312" path="/home/0.8.0b.linux.AMD64/pdb/core/breadboard2.fzp"> <title>Breadboard1</title> <views> <breadboardView layer="breadboardbreadboard"> <geometry z="1.5" x="5" y="250"/> <connectors> <connector connectorId="pin16G" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector1" modelIndex="1768383" layer="breadboard"/> </connects> </connector> <connector connectorId="pin16I" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector0" modelIndex="1768314" layer="breadboard"/> </connects> </connector> <connector connectorId="pin20A" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector0" modelIndex="1768383" layer="breadboard"/> </connects> </connector> <connector connectorId="pin20D" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector1" modelIndex="1768317" layer="breadboard"/> </connects> </connector> <connector connectorId="pin20F" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector0" modelIndex="1768317" layer="breadboard"/> </connects> </connector> <connector connectorId="pin20I" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector1" modelIndex="1768314" layer="breadboard"/> </connects> </connector> </connectors> </breadboardView> <schematicView layer="breadboardbreadboard"> <geometry z="-1" x="0" y="0"/> <connectors> <connector connectorId="pin16G" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector1" modelIndex="1768383" layer="schematic"/> </connects> </connector> <connector connectorId="pin16I" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector0" modelIndex="1768314" layer="schematic"/> </connects> </connector> <connector connectorId="pin20A" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector0" modelIndex="1768383" layer="schematic"/> </connects> </connector> <connector connectorId="pin20D" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector1" modelIndex="1768317" layer="schematic"/> </connects> </connector> <connector connectorId="pin20F" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector0" modelIndex="1768317" layer="schematic"/> </connects> </connector> <connector connectorId="pin20I" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector1" modelIndex="1768314" layer="schematic"/> </connects> </connector> </connectors> </schematicView> <pcbView layer="breadboardbreadboard"> <geometry z="-1" x="0" y="0"/> <connectors> <connector connectorId="pin16G" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector1" modelIndex="1768383" layer="copper0"/> </connects> </connector> <connector connectorId="pin16I" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector0" modelIndex="1768314" layer="copper0"/> </connects> </connector> <connector connectorId="pin20A" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector0" modelIndex="1768383" layer="copper0"/> </connects> </connector> <connector connectorId="pin20D" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector1" modelIndex="1768317" layer="copper0"/> </connects> </connector> <connector connectorId="pin20F" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector0" modelIndex="1768317" layer="copper0"/> </connects> </connector> <connector connectorId="pin20I" layer="breadboardbreadboard"> <geometry x="0" y="0"/> <connects> <connect connectorId="connector1" modelIndex="1768314" layer="copper0"/> </connects> </connector> </connectors> </pcbView> </views> </instance>
    I am getting following error.
    PCB1
    Traceback (most recent call last):
    File "./fritzing_testing.py", line 17, in <module>
    connectors=schview.getElementsByTagName("connectors")[0]
    IndexError: list index out of range

    What could be reason for this.

    Thanks and Regards
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    Usually Japan when not on contract
    Posts
    240
    Rep Power
    11
    Both posted examples are formatted so poorly that I doubt anyone will try to help you.

    Try using a highlight tag on your Python and preserving the indentation (which implies actually including the newlines...) and try running your blob-of-crap XML though "xmllint --format >> pretty.xml" and then try pasting it again (with highlighting, if devshed has an xml highlighter).

IMN logo majestic logo threadwatch logo seochat tools logo