# XPath
- 在XML文件中查找信息的一套规则/语言,根据XML的元素或者属性进行遍历
- http://www.w3school.com.cn/xpath/index.asp
# XPath 开发工具
- 开源的XPath表达式编辑工具:XMLQuire
- Chrome插件:XPath Helper
- Firefox插件: XPath Checker
# 选取节点
- nodename: 选取此节点的所有子节点
- /: 从根节点开始选取
/Student:没有结果
/School:选区School节点
- //: 选取节点,不考虑位置
//age: 选取出三个节点,一般组成列表返回
import xml.dom.minidom
#在内存中创建一个空的文档
doc = xml.dom.minidom.Document()
#创建一个根节点Managers对象
root = doc.createElement('Managers')
#设置根节点的属性
root.setAttribute('company', 'xx科技')
root.setAttribute('address', '科技软件园')
#将根节点添加到文档对象中
doc.appendChild(root)
managerList = [{'name' : 'joy', 'age' : 27, 'sex' : '女'},
{'name' : 'tom', 'age' : 30, 'sex' : '男'},
{'name' : 'ruby', 'age' : 29, 'sex' : '女'}
]
for i in managerList :
nodeManager = doc.createElement('Manager')
nodeName = doc.createElement('name')
#给叶子节点name设置一个文本节点,用于显示文本内容
nodeName.appendChild(doc.createTextNode(str(i['name'])))
nodeAge = doc.createElement("age")
nodeAge.appendChild(doc.createTextNode(str(i["age"])))
nodeSex = doc.createElement("sex")
nodeSex.appendChild(doc.createTextNode(str(i["sex"])))
#将各叶子节点添加到父节点Manager中,
#最后将Manager添加到根节点Managers中
nodeManager.appendChild(nodeName)
nodeManager.appendChild(nodeAge)
nodeManager.appendChild(nodeSex)
root.appendChild(nodeManager)
#开始写xml文档
fp = open('Manager.xml', 'w')
doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
- .: 选取当前接待你
- ..:选取当前节点的父亲节点
- @:选取属性
- xpath中查找一般按照路径方法查找,一下是路径表示方法
School/Teacher:返回Teacher节点
School/Student: 返回两个Student节点
//Student: 选取所有Studetn的节点,不考虑位置
School//Age:选取School后代中所有Age节点
//@Other: 选取Other属性
//Age[@Detail]: 选取带有属性Detail的Age元素
# 谓语-Predicates
- /School/Student[1]: 选取School下面的第一个Student节点
- /School/Student[last()]: 选取School下面的最后一个Student节点
- /School/Student[last()-1]: 选取School下面的倒数第二个Student节点
- /School/Student[position()<3]: 选取School下面的前二个Student节点
- //Student[@score]: 选取带有属性score的Student节点
- //Student[@score="99"]: 选取带有属性score并且属性值是99的Student节点
- //Student[@score]/Age: 选取带有属性score的Student节点的子节点Age
# XPath的一些操作
- |: 或者
//Student[@score] | //Teacher: 选取带有属性score的Student节点和Teahcer节点
- 其余不常见XPath运算符号包括+, - , *, div, >, <