2011-04-20 5 views
2

다음 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() 

가치를 시도했다.

어떻게 속성의 모든 값을 가져 와서 별도의 변수에 저장할 수 있습니까?

+0

저는 미니 돔에 익숙하지 않지만, 알림 목록의 첫 번째 항목 만 요구하기 때문에 처음에는 점점 좋아지고 있다고 생각합니다. 왜 foreach를 설정하지 않습니까? '알림 목록에있는 항목 : ... ' – emragins

답변

0

나열된 출력 값인 xmldoc에서 'notificationlist = xmldoc.getElementsByTagName ('알림 ')'이 생성되었다고 가정하면 네 가지 요소가 있어야합니다. 따라서 notificationlist [0]을 사용하여 요소 0에만 초점을 맞추면 첫 번째 요소 만 처리됩니다. aaa, bbb, ccc, ddd를 앞에 두어 디스크를 다르게 만드는 samlpe xmldoc을 수정 한 코드가 있습니다. 당신이 notificationlist의 각 항목에 대한 속성을 얻고 싶은 경우에 당신은 당신이 할 수있는,

for x in notificationlist: 
    print '*' * 15 
    print x.getElementsByTagName('description').item(0).childNodes[0].data 
    print 
    for y in x.attributes.keys(): 
    print y 
    print x.attributes.getNamedItem(y).nodeValue 
    print '-' *15 



*************** 
     aaaThis is a description 

status 
current 
--------------- 
oid 
1.3.6.1 
--------------- 
name 
ccmSmtp 
--------------- 
*************** 
     bbbThis is a description 

status 
current 
--------------- 
oid 
1.3.6.1 
--------------- 
name 
ccmAlertGroup 
--------------- 
*************** 
    cccThis is a description 

status 
current 
--------------- 
oid 
1.3.6.1 
--------------- 
name 
ccmAlert 
--------------- 
*************** 
     dddThis is a description 

status 
current 
--------------- 
oid 
1.3.6.1 
--------------- 
name 
ccmSmtp 
--------------- 
+0

죄송합니다. – zedman9991

+0

서식이 수정 될 수 있습니다. – jathanism

+0

좋은 지적. 어떻게하는지 알아 냈습니다. – zedman9991

1

--- 인쇄 문을 대체하여 데이터를 캡처 할 수 있습니다 : 여기에서

attrslist = [dict(node.attributes.items()) for node in notificationlist] 
print attrslist[0] 
# => {u'status': u'current', u'oid': u'1.3.6.1', u'name': u'ccmSmtp'} 
print attrslist[0]['status'] 
# => current 

를 그냥 것 이 새 목록을 반복하고 각 <notification> 요소의 이름별로 속성을 가져 오는 것은 notificationlist입니다.

for n in attrslist: 
    status = n['status'] 
    oid = n['oid'] 
    name = n['name'] 
    # blah 
관련 문제