Python 正在根据 IF 条件使用 ElementTree 解析 XML
Python parsing XML using ElementTree based on IF Conditions
使用 Python,我正在尝试解析 XML 文件以根据 'name' 属性的值和子项的值检索元素的值元素也基于 'name' 属性的值。
条件是:
如果 'test' 元素中的 Category 属性为 3,则获取 'name' 属性的值
和
如果 'name' 属性的值为 'enable'
,则获取 'color' 元素的值
示例代码:
<test category="1">
<test category="2">
<test name="1" category="3">
<color name="disable">blue</color>
<color name="disable">yellow</color>
<color name="enable">red</color>
<color name="disable">orange</color>
</test>
<test name="2" category="3">
<color name="disable">green</color>
<color name="disable">purple</color>
<color name="enable">white</color>
<color name="disable">gray</color>
</test>
</test>
</test>
预期结果:
1 红色
2白
我的当前代码:
import xml.etree.ElementTree as ET
tree = ET.parse('C:/colors.xml')
root = tree.getroot()
for test in root.getiterator('test'):
if test.attrib['category']=="3":
print test.attrib['name']
这给了我:
1
2
我尝试了嵌套的 FOR 来获取颜色元素的值,但我尝试的一切似乎都从头开始。
如有任何帮助,我们将不胜感激!
谢谢
- 请记住您的 XML 格式不正确(属性值应在引号中)
- 不匹配
color
标签
您的逻辑使用了代码中缺少的 2 个条件。
string = '''<test category="1">
<test category="2">
<test name="1" category="3">
<color name="disable">blue</color>
<color name="disable">yellow</color>
<color name="enable">red</color>
<color name="disable">orange</color>
</test>
<test name="2" category="3">
<color name="disable">green</color>
<color name="disable">purple</color>
<color name="enable">white</color>
<color name="disable">gray</color>
</test>
</test>
</test>'''
from xml.etree import ElementTree as ET
root = ET.fromstring(string)
for test_node in root.iter('test'):
if test_node.attrib['category'] == "3":
for color_element in test_node.iter('color'):
if color_element.attrib['name'] == 'enable':
print(test_node.attrib['name'], color_element.text)
# 1 red
# 2 white
使用 Python,我正在尝试解析 XML 文件以根据 'name' 属性的值和子项的值检索元素的值元素也基于 'name' 属性的值。
条件是:
如果 'test' 元素中的 Category 属性为 3,则获取 'name' 属性的值
和
如果 'name' 属性的值为 'enable'
,则获取 'color' 元素的值示例代码:
<test category="1">
<test category="2">
<test name="1" category="3">
<color name="disable">blue</color>
<color name="disable">yellow</color>
<color name="enable">red</color>
<color name="disable">orange</color>
</test>
<test name="2" category="3">
<color name="disable">green</color>
<color name="disable">purple</color>
<color name="enable">white</color>
<color name="disable">gray</color>
</test>
</test>
</test>
预期结果:
1 红色
2白
我的当前代码:
import xml.etree.ElementTree as ET
tree = ET.parse('C:/colors.xml')
root = tree.getroot()
for test in root.getiterator('test'):
if test.attrib['category']=="3":
print test.attrib['name']
这给了我:
1
2
我尝试了嵌套的 FOR 来获取颜色元素的值,但我尝试的一切似乎都从头开始。
如有任何帮助,我们将不胜感激!
谢谢
- 请记住您的 XML 格式不正确(属性值应在引号中)
- 不匹配
color
标签
您的逻辑使用了代码中缺少的 2 个条件。
string = '''<test category="1">
<test category="2">
<test name="1" category="3">
<color name="disable">blue</color>
<color name="disable">yellow</color>
<color name="enable">red</color>
<color name="disable">orange</color>
</test>
<test name="2" category="3">
<color name="disable">green</color>
<color name="disable">purple</color>
<color name="enable">white</color>
<color name="disable">gray</color>
</test>
</test>
</test>'''
from xml.etree import ElementTree as ET
root = ET.fromstring(string)
for test_node in root.iter('test'):
if test_node.attrib['category'] == "3":
for color_element in test_node.iter('color'):
if color_element.attrib['name'] == 'enable':
print(test_node.attrib['name'], color_element.text)
# 1 red
# 2 white