libxml2 has a number of advantages:
- Compliance to the spec
- Active development and a community participation
- Speed. This is really a python wrapper around a C implementation.
- Ubiquity. The libxml2 library is pervasive and thus well tested.
डाउनसाइड्स में शामिल हैं:
- spec का अनुपालन। यह सख्त है। डिफ़ॉल्ट पुस्तकालय हैंडलिंग जैसी चीजें अन्य पुस्तकालयों में आसान हैं।
- मूल कोड का उपयोग करें। यह आपके दर्द को वितरित/तैनात करने के तरीके के आधार पर दर्द हो सकता है। आरपीएम उपलब्ध हैं जो इस दर्द में से कुछ को कम करते हैं।
- मैन्युअल संसाधन हैंडलिंग। FreeDoc() और xpathFreeContext() पर कॉल के नीचे नमूने में नोट करें। यह बहुत पाइथोनिक नहीं है।
यदि आप सरल पथ चयन कर रहे हैं, तो ElementTree (जो पायथन 2.5 में शामिल है) के साथ चिपके रहें । यदि आपको पूर्ण स्पेक अनुपालन या कच्ची गति की आवश्यकता है और मूल कोड के वितरण से निपट सकते हैं, libxml2 के साथ जाएं।
libxml2 XPath उपयोग का नमूना
import libxml2
doc = libxml2.parseFile("tst.xml")
ctxt = doc.xpathNewContext()
res = ctxt.xpathEval("//*")
if len(res) != 2:
print "xpath query: wrong node set size"
sys.exit(1)
if res[0].name != "doc" or res[1].name != "foo":
print "xpath query: wrong node set value"
sys.exit(1)
doc.freeDoc()
ctxt.xpathFreeContext()
एलिमेंट ट्री XPath उपयोग का नमूना
from elementtree.ElementTree import ElementTree
mydoc = ElementTree(file='tst.xml')
for e in mydoc.findall('/foo/bar'):
print e.get('title').text