读书人

小弟我用minidom解析一个很简单的xml不

发布时间: 2012-04-08 14:38:30 作者: rapoo

我用minidom解析一个很简单的xml不成功啊?
我用python2.6.4,写了这么一个python程序

Python code
from xml.dom import minidomtry:    xmlfile = open("d:\\File1.xml", "a+")    xmldoc = minidom.parse(xmlfile)except :    sys.exit(0)Info = xmldoc.getElementsByTagName('Request_Info')[0]Attr = Info.attributes['Request_Type'];Type = Attr.value;print(Type)

运行报错:
>>>
Traceback (most recent call last):
File "D:\my.py", line 8, in <module>
Attr = Info.attributes['Request_Type'];
File "C:\Python26\lib\xml\dom\minidom.py", line 530, in __getitem__
return self._attrs[attname_or_tuple]
KeyError: 'Request_Type'

我要解析的文件如下:
XML code
<?xml version="1.0" encoding="utf-8" ?><TT_Msg>  <Request_Info>    <Request_Type>XML</Request_Type>    <Result>      <NewDataSet>        <RecordsTable>          <COMPONENT>MyComponent</COMPONENT>          <DEVELOPER>MyName</DEVELOPER>          <TITLE>When server starts, service crashes</TITLE>          <ISSUEID>123456</ISSUEID>          <ID>123456</ID>        </RecordsTable>        <MoreInfoTable>          <MoreRecords>False</MoreRecords>          <ID>456</ID>        </MoreInfoTable>      </NewDataSet>    </Result>    <Msg></Msg>  </Request_Info></TT_Msg>


错在哪里,怎么改?

[解决办法]
<Request_Type>XML</Request_Type>是Request_Info的一个子节点,不是它的attribute.

不清楚你想要的结果,不知道要怎么改。

读书人网 >perl python

热点推荐