XML parse into array in python -


मेरे पास XML है:

  & lt;? Xml संस्करण = "1.0"? & gt; & Lt; iq id = "123" से = "परीक्षण" प्रकार = "परिणाम" & gt; & Lt; क्वेरी xmlns = "jabber: iq: रोस्टर" & gt; & Lt; आइटम jid = "foo" सदस्यता = "both" / & gt; & Lt; आइटम jid = "bar" subscription = "both" / & gt; & Lt; / क्वेरी & gt; & Lt; / बुद्धि & gt;   

और मैं मद से सरणी में जेड को पार्स करना चाहता हूं। मैंने सोचा कि ऐसा कुछ काम करेगा

  import xml.etree.ElementTree के रूप में एट मेरीर = [] xml = '& lt;? Xml संस्करण = "1.0"? & Gt; & lt; iq id = " 123 "से =" परीक्षण "प्रकार =" परिणाम "& gt; & lt; क्वेरी xmlns =" ​​jabber: iq: रोस्टर "& gt; & lt; आइटम jid =" foo "subscription =" both "/ & gt; & lt; आइटम jid =" बार "सदस्यता =" दोनों "/> gt; & lt; / query & gt; & lt; / iq & gt; रूट = ईटी.फ्रेमस्ट्रिंग (xml) root.findall ('query') में आइटम के लिए: t = item.get ('jid') myarr.append (t) प्रिंट (टी)    

आपको नामस्थानों को संभालना चाहिए। एक विकल्प नामस्थान को एक्सपेथ अभिव्यक्ति में पेस्ट करना होता है:

  आइटम के लिए root.findall में ('.// ​​{% (ns) s} क्वेरी / {% (ns) s} आइटम '% {' Ns ':' jabber: iq: roster '}): t = item.attrib.get (' jid ') myarr.append (t) प्रिंट (टी)   

प्रिंट:

  foo बार   

इन्हें भी देखें:



Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -