다음 xml을 다른 xml 파일에서 추출했습니다.Python을 사용하여 XML에서 값 추출
<notifications>
<notification name="ccmSmtp" oid="1.3.6.1" status="current">
<objects>
<object module="callhome" name="ccmSmtp" />
</objects>
<description>This is a description</description>
</notification>
<notification name="ccmAlertGroup" oid="1.3.6.1" status="current">
<objects>
<object module="callhome" name="callHome" />
</objects>
<description>This is a description</description>
</notification>
<notification name="ccmAlert" oid="1.3.6.1" status="current">
<objects>
<object module="callhome" name="callHome" />
</objects>
<description>This is a description</description>
</notification>
<notification name="ccmSmtp" oid="1.3.6.1" status="current">
<objects></objects>
<description>This is a description</description>
</notification>
</notifications>
다음 파이썬 코드를 사용하고 있습니다.
from xml.dom import minidom
xmldoc = minidom.parse('example.xml')
grammarNode = xmldoc.childNodes[2]
notificationsNode = grammarNode.childNodes[9]
print notificationsNode.toxml()
이 파이썬 코드는 위에 주어진 xml의 결과를 제공합니다.
나는 속성을 얻기 위해 다음 내가 통지의 주먹 세트의 값을 얻을 수있어이 사용notificationlist = xmldoc.getElementsByTagName('notification')
print notificationlist[0].toxml()
notification1 = notificationlist[0]
key = notification1.attributes.keys()
가치를 시도했다.
어떻게 속성의 모든 값을 가져 와서 별도의 변수에 저장할 수 있습니까?
저는 미니 돔에 익숙하지 않지만, 알림 목록의 첫 번째 항목 만 요구하기 때문에 처음에는 점점 좋아지고 있다고 생각합니다. 왜 foreach를 설정하지 않습니까? '알림 목록에있는 항목 : ... ' – emragins