I'm from a VBScript background and new to lxml with Python.
In VBScript, to choose a specific node, I would simply do something like:
Set myNode = xmlDoc.selectSingleNode("/node1/node2/myNode")
.
What I have done with Python:
from lxml import etree
xmlDoc = etree.parse(fileName)
myNode =
Question : So what should be written in front of myNode
to be able to select it?
Preferably without using XPath ? Also taking lxml into account
You could use something like:
myNode = xmlDoc.find('node2/myNode')
The etree.parse
function will return a root node (ie your node1
), so you don't need to use an absolute path.
Example
content = '''
<root>
<div>
<p>content 1</p>
</div>
</root>
'''
from lxml import etree
xmlDoc = etree.fromstring(content)
paragraph_element = xmlDoc.find('div/p')
print(paragraph_element)
Output
<Element p at 0x9f54bc8>
Note: For my example I have used the function etree.fromstring
. This is purely for demonstration purposes, so you can see a workable example using a string. The function etree.parse
should generate the same result when working with files rather than strings.
Aside: Why not use XPath? It is extremely powerful!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.