仅列出一类Python xml

仅列出一类Python xml,python,xml,dom,lxml,minidom,Python,Xml,Dom,Lxml,Minidom,我正在尝试编写一个python程序,它使用DOM读取xml文件并打印另一个xml结构,该结构只从一个节点列出特定的选定属性“fun” 你们能帮我调试代码吗?使用getAttribute: for n in dom.getElementsByTagName('url'): if (n.getAttribute('category') == "fun"): print(n.toxml()) 注意:你的一个标签会打开“网站”并试图关闭“网站”-所以你会想修复这个标签 您已经提

我正在尝试编写一个python程序,它使用DOM读取xml文件并打印另一个xml结构,该结构只从一个节点列出特定的选定属性“fun”


你们能帮我调试代码吗?

使用
getAttribute

for n in dom.getElementsByTagName('url'):
    if (n.getAttribute('category') == "fun"):
        print(n.toxml())

注意:你的一个标签会打开“网站”并试图关闭“网站”-所以你会想修复这个标签

您已经提到了
lxml

from lxml import etree as et

root = et.fromstring(xml)
fun = root.xpath('/Website/url[@category="fun"]')
for node in fun:
    print et.tostring(node)

谢谢你,乔恩!但是我在python中使用minidom从xml中提取数据。
from lxml import etree as et

root = et.fromstring(xml)
fun = root.xpath('/Website/url[@category="fun"]')
for node in fun:
    print et.tostring(node)