语法错误:在 Python 中解析 xml 文件时谓词无效

问题描述 投票:0回答:1

我正在编写一个 python 脚本,其目标是在匹配给定条件时更改 xml 标签的值:在这里,如果 id 是“皮卡丘”,我想将活动标签从 true 更改为 false

当我运行 python 脚本时,出现 SyntaxError: invalid predicate 错误。

这是我的Python示例代码

import xml.etree.ElementTree as ET

def engineXmlCleaning(bs):
     enginePath = "Engine.xml"
     tree = ET.parse(enginePath)
     root = tree.getroot()
     
     id_to_check='pikachu'

     # Search for the element with specified ID
     matching_elements = root.findall(".//tototiti.engine.BestMatchingConfig[bestMatchingDataID/id='" + id_to_check + "']")

     # Check if element exists 
     if matching_elements:
          print("The element with id " + id_to_check + " exists.")

     for element in matching_elements:
          active_element = element.find('.//active')
          if active_element is not None:
               print('... Amending the Active value ...')
               active_element.text = 'false'

     tree.write(enginePath)

这是我想通过脚本修改的 xml 文件示例

<Engine id="1">
<bestMatchingConfigs id="50">
        <tototiti.engine.BestMatchingConfig id="51">
            <ParsingTypeName>alex_ordinate</ParsingTypeName>
            <bestMatchingDataID id="52">
                <id>pikachu</id>
            </bestMatchingDataID>
            <scriptID id="53">
                <id>pika_js</id>
            </scriptID>
            <active>true</active>
        </tototiti.engine.BestMatchingConfig>
        <tototiti.engine.BestMatchingConfig id="54">
            <ParsingTypeName>don_pillar</ParsingTypeName>
            <bestMatchingDataID id="55">
                <id>don</id>
            </bestMatchingDataID>
            <scriptID id="56">
                <id>don_js</id>
            </scriptID>
            <active>true</active>
        </tototiti.engine.BestMatchingConfig>
</bestMatchingConfigs>
</Engine>

当我运行 python 脚本时,出现 SyntaxError: invalid predicate 错误。

python xml elementtree
1个回答
0
投票

您可以使用 if 条件和 find():

import xml.etree.ElementTree as ET

root = ET.parse('engine.xml').getroot()

for toto in root.findall('.//tototiti.engine.BestMatchingConfig'):
    if toto.find('bestMatchingDataID/id').text == 'pikachu':
        toto.find('active').text = 'false'

ET.indent(root, space="  ")
ET.dump(root)

输出:

<Engine id="1">
  <bestMatchingConfigs id="50">
    <tototiti.engine.BestMatchingConfig id="51">
      <ParsingTypeName>alex_ordinate</ParsingTypeName>
      <bestMatchingDataID id="52">
        <id>pikachu</id>
      </bestMatchingDataID>
      <scriptID id="53">
        <id>pika_js</id>
      </scriptID>
      <active>false</active>
    </tototiti.engine.BestMatchingConfig>
    <tototiti.engine.BestMatchingConfig id="54">
      <ParsingTypeName>don_pillar</ParsingTypeName>
      <bestMatchingDataID id="55">
        <id>don</id>
      </bestMatchingDataID>
      <scriptID id="56">
        <id>don_js</id>
      </scriptID>
      <active>true</active>
    </tototiti.engine.BestMatchingConfig>
  </bestMatchingConfigs>
</Engine>

对于 xpath 搜索,您可以使用

xml.etree.ElementTree
中的部分:

# Search for the element with specified ID
matching_elements = root.findall('.//*[id="pikachu"]/id')

# Check if element exists
if id_to_check == matching_elements[0].text:
    print("The element with id " + id_to_check + " exists.")

© www.soinside.com 2019 - 2024. All rights reserved.