python3 xpath_Python 3中用XPath解析XML
我有以下xml:
Biscuits Wrapped
Finished
Biscuits
Biscuits (Wrapped)
Biscuits Sweet
Finished
Biscuits
Biscuits (Sweets)
Biscuits Savoury
Finished
Biscuits
Biscuits For Cheese
....
我已使用以下代碼將其加載到樹中:try:
groups = etree.parse(PRODUCT_GROUPS_XML_FILEPATH)
root = groups.getroot()
internalGroup = root.findall("./internal-code")
LOG.append("[INFO] product groupings file loaded and parsed ok")
except Exception as e:
LOG.append("[ERROR] PRODUCT GROUPINGS XML FILE ACCESS PROBLEM")
LOG.append("[***TERMINATED***]")
writelog()
exit()
我想使用XPath找到正確的,然后能夠訪問該組的子節點。因此,如果我正在搜索內部代碼221并想要網絡類別,我將執行以下操作:internalGroup.find("internal-code", 221).get("web-category").text
我對XML和Python沒有經驗,我已經盯著它看了很久了。所有的幫助都得到了非常感激的接待。謝謝
總結
以上是生活随笔為你收集整理的python3 xpath_Python 3中用XPath解析XML的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 的写法_横、竖的写法
- 下一篇: 450g带盖吐司配方_450g带盖吐司烤