访问位于根元素之前的 XML 注释

发布于 2024-08-17 19:16:11 字数 204 浏览 3 评论 0原文

请帮我解决 lxml 的问题。 如何从此文件中获取“注释 1”?

<?xml version="1.0" encoding="windows-1251" standalone="yes" ?>
<!--Comment 1-->
<a>
   <!--Comment 2-->
</a>

Please help me to resolve my problem with lxml.
How can I get "Comment 1" from this file?

<?xml version="1.0" encoding="windows-1251" standalone="yes" ?>
<!--Comment 1-->
<a>
   <!--Comment 2-->
</a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-08-24 19:16:11

文档:lxml 教程,然后搜索“注释”

代码:

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

Docs: the lxml tutorial, and search for "Comments"

Code:

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)

Output:

=== 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'

Comments: doesn't work with xml.etree.cElementTree

人间不值得 2024-08-24 19:16:11
>>> from lxml import etree
>>> tree = etree.parse('filename.xml')
>>> root = tree.getroot()
>>> print root.getprevious()
<!--Comment 1-->

或者可以肯定的是(可能有多个):

>>> for i in root.itersiblings(tag=etree.Comment, preceding=True):
...     print i
...
<!--Comment 1-->

如果您想提取评论的文本,请使用 .text 属性。

>>> from lxml import etree
>>> tree = etree.parse('filename.xml')
>>> root = tree.getroot()
>>> print root.getprevious()
<!--Comment 1-->

Or to be sure (there might be more than one):

>>> for i in root.itersiblings(tag=etree.Comment, preceding=True):
...     print i
...
<!--Comment 1-->

Use the .text attribute if you want to extract the text of the comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文