简体   繁体   English

使用Python LXML读取xml文件

[英]Reading xml file using Python lxml

I'm reading the below xml file using Python lxml but could not get to the node 我正在使用Python lxml读取下面的xml文件,但无法到达节点

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<licenses>
    <license>
        <serial id>1234</serialid>
        <key>
            <Product>Test Producet</Product>
            <Start>Jan</Start>
        <key>
    </license>
</licenses>

Python code Python代码

tree = etree.parse('test.slf')
root = tree.getroot()
print 'root = ', root[0][0].findall('key')
#print 'root findall = ', root.getroottree('key')
for node in root.findall('key'):
        print node
for node in tree.xpath('//key'):
    print node

btw, there are 2 typos in your xml: take out the space in opening tag of <serial id> and add / to closing <key> 顺便说一句,您的xml中有2种错别字:取出<serial id>开始标记中的空格,并添加/到结束<key>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM