我有這樣的XML:蟒蛇minidom命名讀取XML
<?DOMParser ?>
<logbook:LogBook xmlns:logbook="http://www/logbook/1.0" version="1.2">
<visits>
<visit>
<general>
<startDateTime>2014-01-10T12:22:39.166</startDateTime>
<endDateTime>2014-03-11T13:51:31.480</endDateTime>
</general>
</visit>
<visit>
<general>
<startDateTime>2013-01-10T12:22:39.166</startDateTime>
<endDateTime>2013-03-11T13:51:31.480</endDateTime>
</general>
</visit>
</visits>
</logbook:LogBook>
我想有從XML輸出:
startDateTime | endDateTime |
-----------------------|-----------------------|
2014-01-10T12:22:39.166|2014-03-11T13:51:31.480|
-----------------------|-----------------------|
2013-01-10T12:22:39.166|2013-03-11T13:51:31.480|
我使用minidom
,所以我寫了:
import xml.dom.minidom as minidom
doc=minidom.parse('test.xml')
general=doc.getElementsByTagName('general')[0]
startDateTime=general.getAttribute('startDateTime')
print(startDateTime)
這將返回空字符串給我。這裏有什麼幫助?
'startDateTime'是'general',不是一個屬性的子元素。 – mzjn
@mzjn你是對的。謝謝 – Safariba
我寫道:一般= doc.getElementsByTagName('startDateTime')[0] .firstChild.data – Safariba