根据属性去找这个元素,然后获取它的另一个属性值
def GetElement(TypeName,TypeValue): self.GetActivitXml("MarketTest") tree = ET.ElementTree(file=PATH("MarketTest.xml")) Elements = tree.iter(tag="node") for Element in Elements: if Element.attrib[TypeName] == TypeValue: bounds = Element.attrib["bounds"]
根据路径查找到元素
def GetXpathElement(xpath): self.GetActivitXml("MarketTest") tree = ET.ElementTree(file=PATH("MarketTest.xml")) root = tree.getroot() for index in xpath: root = root[index]
XPath 语法
语法 | 解释 |
---|---|
tag | 选中符合给定tag的全部孩子elements。 For example, spam selects all child elements named spam, and spam/egg selects all grandchildren named egg in all children namedspam. |
* | 选中全部孩子elements。 For example, */egg selects all grandchildren named egg. |
. | 选中当前element。 This is mostly useful at the beginning of the path, to indicate that it’s a relative path. |
// | 选中同一级别的全部子element. For example, .//egg selects all egg elements in the entire tree. |
.. | 选中父亲节点; |
[] | 选中含有给定属性的全部节点。 |
[@attrib='value'] | 选中含有给定属性以及给定属性值的全部节点。The value cannot contain quotes. |
[tag] | 选中所有拥有一个叫做tag的孩子的elements。 Only immediate children are supported. |
[position] | 选中所有位于指定位置的elements。 The position can be either an integer (1 is the first position), the expression last() (for the last position), or a position relative to the last position (e.g. last()-1). |