python 3中的Xpath等价物
我一直在尝试使用xpath的等价物来解析python 3中dom对象的xpath表达式 目前,我在python 2.6中有以下代码:python 3中的Xpath等价物,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我一直在尝试使用xpath的等价物来解析python 3中dom对象的xpath表达式 目前,我在python 2.6中有以下代码: import xml.dom.minidom as dom import xpath xpath_expr = "//application.../files/file" input_xml = "sample xml" doc = dom.parseString(input_xml) elements = xpath.find(xpath_expr, do
import xml.dom.minidom as dom
import xpath
xpath_expr = "//application.../files/file"
input_xml = "sample xml"
doc = dom.parseString(input_xml)
elements = xpath.find(xpath_expr, doc)
这些要素包括:
[<DOM Element: file at 0x7f06d9d25e60>, <DOM Element: file at 0x7f06d9d297a0>]
[<Element file at 0x7f5098c795f0>, <Element file at 0x7f5098c79590>]
这些要素包括:
[<DOM Element: file at 0x7f06d9d25e60>, <DOM Element: file at 0x7f06d9d297a0>]
[<Element file at 0x7f5098c795f0>, <Element file at 0x7f5098c79590>]
[,]
但是,我希望输出数组的元素类型为DOM-Element
,而不是Element
from xml.dom.minidom import parseString
from lxml import etree
input_string = ""
root = etree.fromstring(input_string)
xpath_expr = "//application"
elements = root.xpath(xpath_expr)
dom_elements = []
for element in elements:
dom_elements.append(parseString(etree.tostring(element)).getElementsByTagName("*")[0])
打印dom\u元素
[, ]
DOM元素和元素
有什么不同?可能没有什么不同。如果您确实需要DOM元素
,那么不要使用lxml
——更改xpath\u expr