Xml数据格式
格式示例:
json = {
"name":"永辉超市",
"address": "肖家河大厦",
"goods": [
{
"name": "泡面", "price":"3.5", "count": 20},
{
"name": "矿泉水", "price":"2", "count": 15},
{
"name": "面包", "price":"5", "count": 50}
]
}
xml:
<supermarket>
<name>永辉超市</name>
<address>肖家河大厦</address>
<goodslist>
<goods name="泡面" price="3.5" count="20"></goods>
<goods name="矿泉水" price="2" count="15"></goods>
<goods name="面包" price="5" count="50"></goods>
</goodslist>
</supermarket>
创建树对象获取根节点
supermarket = etree.XML(xml_str)
print(supermarket) # <Element supermarket at 0x16e934f9700>
获取标签(获取节点)
-
节点对象.xpath(路径) - 根据路径找到队形的节点,返回节点对象
-
绝对路径:不管xpath前面的节点对象是什么,路径从根节点开始写
# 写法:/绝对路径
worker_list = supermarket.xpath('/supermarket/worker_list')
print(worker_list) # [<Element worker_list at 0x16e93728640>]
cashier = supermarket.xpath('/supermarket/worker_list/cashier')
print(cashier) # [<Element cashier at 0x16e93728740>]
- ./相对路径:用.(点)来表示当前节点,xpath前面是谁,当前节点就是谁
cashier = supermarket.xpath('./worker_list/cashier')
print(cashier) # [<Element cashier at 0x16e93728740>]
cashier = worker_list[