python中缺少xml元素

python中缺少xml元素,python,xml,sqlite,minidom,Python,Xml,Sqlite,Minidom,系统在Python2.7.2中使用dom解析器。目标是提取.db文件并在sql server上使用它。我读过类似的问题/答案,关于如何在解析xml文件时处理缺少的元素。但我仍然无法找到解决方案。xml有15000多个元素。以下是xml的基本代码: <topo> <vlancard> <id>4545</id> <nodeValue>21</nodeValue> <vlanNam

系统在Python2.7.2中使用dom解析器。目标是提取.db文件并在sql server上使用它。我读过类似的问题/答案,关于如何在解析xml文件时处理缺少的元素。但我仍然无法找到解决方案。xml有15000多个元素。以下是xml的基本代码:

<topo>
   <vlancard>
      <id>4545</id>
      <nodeValue>21</nodeValue>
      <vlanName>voice</vlanName>
   </vlancard>
   <vlancard>
      <id>1234</id>
      <nodeValue>42</nodeValue>
      <vlanName>camera</vlanName>
   </vlancard>
   <vlancard>
      <id>9876</id>
      <nodeValue>84</nodeValue>
   </vlancard>
</topo>
运行模块后:

IndexError: list index out of range
>>> len(id)
16163
>>> len(vlanName)
16155
由于这个问题,元素的排序出现了问题。在打印表时,解析器传递缺少的元素,元素顺序混淆。我使用一个简单的while循环将值插入表中

x=0
while x < (len(vlId)):
    c.execute('''insert into vlan ('id','nodeValue','vlanName') values ('%s','%s','%s') ''' %(id[x].firstChild.nodeValue, nodeValue[x].firstChild.nodeValue, vlanName[x].firstChild.nodeValue))
    x= x+1
x=0
当x<(len(vlId)):
c、 执行(''insert into vlan('id','nodeValue','VLANAME')值('%s','%s','%s')''''''''''%s')”””((id[x].firstChild.nodeValue,nodeValue[x].firstChild.nodeValue,VLANAME[x].firstChild.nodeValue))
x=x+1
我还能怎么做?任何帮助都将不胜感激


Yusuf

不是解析整个xml然后插入,而是解析每个VLAN卡,检索它的id/值/名称,然后将它们插入数据库。

以这种方式,我如何分离缺少的节点?Python认为它是NULL,没有或“你将从Python得到一个<代码> NO.< /代码>,在这种情况下,你应该将适当的NULL插入到该元素的DB中。
x=0
while x < (len(vlId)):
    c.execute('''insert into vlan ('id','nodeValue','vlanName') values ('%s','%s','%s') ''' %(id[x].firstChild.nodeValue, nodeValue[x].firstChild.nodeValue, vlanName[x].firstChild.nodeValue))
    x= x+1