- 论坛徽章:
- 0
|
10可用积分
我用python2.6.4,写了这么一个python程序
- from xml.dom import minidom
- try:
- 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 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,通过以下语句可以实现:
|