डॉक्स: the lxml tutorial, और के लिए "टिप्पणी" खोज
कोड:
import lxml.etree as et
text = """\
<?xml version="1.0" encoding="windows-1251" standalone="yes" ?>
<!--Comment 1a-->
<!--Comment 1b-->
<a> waffle
<!--Comment 2-->
blah blah
</a>
<!--Comment 3a-->
<!--Comment 3b-->
"""
print "\n=== %s ===" % et.__name__
root = et.fromstring(text)
for pre in (True, False):
for comment in root.itersiblings(tag=et.Comment, preceding=pre):
print pre, comment
for elem in root.iter():
print
print isinstance(elem.tag, basestring), elem.__class__.__name__, repr(elem.tag), repr(elem.text), repr(elem.tail)
आउटपुट:
=== lxml.etree ===
True <!--Comment 1b-->
True <!--Comment 1a-->
False <!--Comment 3a-->
False <!--Comment 3b-->
True _Element 'a' ' waffle\n ' None
False _Comment <built-in function Comment> 'Comment 2' '\n blah blah\n'
टिप्पणियाँ: xml.etree.cElementTree
साथ काम नहीं करता
स्रोत
2010-01-14 11:17:20
आईआईआरसी, टिप्पणी 1 एक्सएमएल पार्सर के लिए सुलभ नहीं है क्योंकि यह एक टिप्पणी है। आपको केवल पाठ के रूप में फ़ाइल को पढ़ना पड़ सकता है। – KevinDTimm
क्या आप उत्तर स्वीकार करने की योजना बना रहे हैं? –