आप lxml library उपयोग कर सकते हैं। objectify.fromstring
का उपयोग करके स्ट्रिंग को xml ऑब्जेक्ट में कनवर्ट करें और फिर ऑब्जेक्ट डीआईआर विधि देखें। उदाहरण के लिए:
from lxml import objectify
xml_string = """<?xml version="1.0" encoding="UTF-8"?><NewOrderResp><IndustryType></IndustryType><MessageType>R</MessageType><MerchantID>700000005894</MerchantID><TerminalID>0031</TerminalID><CardBrand>AMEX</CardBrand><AccountNum>3456732800000010</AccountNum><OrderID>TESTORDER1</OrderID><TxRefNum>55A69B278025130CD36B3A95435AA84DC45363</TxRefNum><TxRefIdx>10</TxRefIdx><ProcStatus>0</ProcStatus><ApprovalStatus>1</ApprovalStatus><RespCode></RespCode><AVSRespCode></AVSRespCode><CVV2RespCode></CVV2RespCode><AuthCode></AuthCode><RecurringAdviceCd></RecurringAdviceCd><CAVVRespCode></CAVVRespCode><StatusMsg></StatusMsg><RespMsg></RespMsg><HostRespCode></HostRespCode><HostAVSRespCode></HostAVSRespCode><HostCVV2RespCode></HostCVV2RespCode><CustomerRefNum>A51C5B2B1811E5991208</CustomerRefNum><CustomerName>BOB STEVEN</CustomerName><ProfileProcStatus>0</ProfileProcStatus><CustomerProfileMessage>Profile Created</CustomerProfileMessage><RespTime>13055</RespTime><PartialAuthOccurred></PartialAuthOccurred><RequestedAmount></RequestedAmount><RedeemedAmount></RedeemedAmount><RemainingBalance></RemainingBalance><CountryFraudFilterStatus></CountryFraudFilterStatus><IsoCountryCode></IsoCountryCode></NewOrderResp>"""
xml_object = objectify.fromstring(xml_string)
print xml_object.__dict__
dict के लिए एक्सएमएल वस्तु परिवर्तित एक dict वापसी होगी:
{'RemainingBalance': u'', 'AVSRespCode': u'', 'RequestedAmount': u'', 'AccountNum': 3456732800000010, 'IsoCountryCode': u'', 'HostCVV2RespCode': u'', 'TerminalID': 31, 'CVV2RespCode': u'', 'RespMsg': u'', 'CardBrand': 'AMEX', 'MerchantID': 700000005894, 'RespCode': u'', 'ProfileProcStatus': 0, 'CustomerName': 'BOB STEVEN', 'PartialAuthOccurred': u'', 'MessageType': 'R', 'ProcStatus': 0, 'TxRefIdx': 10, 'RecurringAdviceCd': u'', 'IndustryType': u'', 'OrderID': 'TESTORDER1', 'StatusMsg': u'', 'ApprovalStatus': 1, 'RedeemedAmount': u'', 'CountryFraudFilterStatus': u'', 'TxRefNum': '55A69B278025130CD36B3A95435AA84DC45363', 'CustomerRefNum': 'A51C5B2B1811E5991208', 'CustomerProfileMessage': 'Profile Created', 'AuthCode': u'', 'RespTime': 13055, 'HostAVSRespCode': u'', 'CAVVRespCode': u'', 'HostRespCode': u''}
एक्सएमएल स्ट्रिंग मैं इस्तेमाल किया सिर्फ एक वास्तविक दुनिया उदाहरण दिखाने के लिए प्रवेश द्वार Paymentech भुगतान से एक प्रतिक्रिया है।
यह भी ध्यान दें कि उपरोक्त उदाहरण रिकर्सिव नहीं है, इसलिए यदि डिक्ट्स के अंदर डिक्ट्स हैं तो आपको कुछ रिकर्सन करना होगा। पुनरावर्ती क्रिया मैंने लिखा है कि आप उपयोग कर सकते हैं:
from lxml import objectify
def xml_to_dict_recursion(xml_object):
dict_object = xml_object.__dict__
if not dict_object:
return xml_object
for key, value in dict_object.items():
dict_object[key] = xml_to_dict_recursion(value)
return dict_object
def xml_to_dict(xml_str):
return xml_to_dict_recursion(objectify.fromstring(xml_str))
xml_string = """<?xml version="1.0" encoding="UTF-8"?><Response><NewOrderResp>
<IndustryType>Test</IndustryType><SomeData><SomeNestedData1>1234</SomeNestedData1>
<SomeNestedData2>3455</SomeNestedData2></SomeData></NewOrderResp></Response>"""
print xml_to_dict(xml_string)
यहाँ
एक संस्करण है कि माता-पिता कुंजी/तत्व को बरकरार रखता है:
def xml_to_dict(xml_str):
""" Convert xml to dict, using lxml v3.4.2 xml processing library, see http://lxml.de/ """
def xml_to_dict_recursion(xml_object):
dict_object = xml_object.__dict__
if not dict_object: # if empty dict returned
return xml_object
for key, value in dict_object.items():
dict_object[key] = xml_to_dict_recursion(value)
return dict_object
xml_obj = objectify.fromstring(xml_str)
return {xml_obj.tag: xml_to_dict_recursion(xml_obj)}
और आप केवल एक सबट्री लौट सकते हैं और dict करने के लिए इसे परिवर्तित करना चाहते हैं, तो , आप Element.find (उपयोग कर सकते हैं):
xml_obj.find('.//') # lxml.objectify.ObjectifiedElement instance
कई विकल्प यह पूरा करने के हैं, लेकिन यह एक महान अगर आप पहले से lxml का उपयोग कर रहे है। इस उदाहरण में lxml-3.4.2 का उपयोग किया गया था। चेयर!
ऐसा प्रतीत होता है कि इस प्रश्न का उत्तर पहले दिया गया है: http://stackoverflow.com/questions/2148119/how-to-convert-an-xml-string-to-a-dictionary-in-python – robjohncox
आपका वांछित आउटपुट क्या है ? – alecxe
@ जोश मैं आपके विचार को नहीं समझता – funktasmas