无法在 python xml.etree.ElementTree 中找到访问 XPath 中的元素

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

我一直在兜圈子。我的任务是读取 xml 文档。解析记录以查找 ID。使用该 ID 执行一些 SQL。然后将 xml 的值与 SQL 的值进行比较。如果 SQL 不同(且不为空),我们会更新 XML 以发送回另一台服务器。

一切正常,除了当我尝试获取 XML 记录中字段的值时,它没有找到。

我创建了两个 xml 映射字典:

xml_sql_mapping_sa = {
    'ncaaId': 'RZECAST_KEYNCAAID',
    'schoolSid': 'SPRIDEN_ID',
    'birthDate': 'BIRTHDATE',
    'email': 'EMAIL',
    'ethnicCode': 'ETHNICODE',
    'firstName': 'FIRSTNAME',
    'lastName': 'LASTNAME',
    'MI': 'MI',
    'gender': 'GENDER',
    'primaryPhone': 'PRIMARY_PHONE'
}

# Define a mapping between XML fields and SQL columns for <parent> elements
xml_sql_mapping_parent = {
    'email': 'PARENT_EMAIL',
    'primaryPhone': 'PARENT_PHONE'
}

这是 XML 的结构:

<students>
<sa birthDate="2005-##-####" email="[email protected]" ethnicCode="5" firstName="L____" gender="F" lastName="A____d" ncaaId="211123456908" primaryPhone="208-555-5555" schoolSid="020111126">
<saDetail fulltimeEnrollmentTermAny="S1" fulltimeEnrollmentTermHere="S1" fulltimeEnrollmentYearAny="2024" fulltimeEnrollmentYearHere="2024" internationalFlag="N"/>
<address address1="678 Address" city="theCity" country="US" postalCode="83204" state="ID"/>
<parent email="[email protected]" name="first Name" primaryPhone="25555541522"/>
<saPreFte hoursCode="ADVANCED_PLACEMENT"/>
<saPreFte degreeApplicableHours="27.0" earnedHours="27.0" hoursCode="CREDIT_BEFORE_FULL_TIME"/>
<saPreFte hoursCode="CREDIT_BY_EXAM"/>
<saPreFte hoursCode="SUMMER_BRIDGE"/>
<saYear academicYear="2024">
<saYearSport sportCode="WGO"/>
<saYearEligible financialAidCertDate="2023-08-09" medicalDate="2023-08-08"/>
<saYearTerm termCode="S1"/>
<saYearTerm termCode="S2"/>
<saYearTerm termCode="SU"/>
<saYearPtd classYear="1"/>
</saYear>
</sa>
</students>

相关代码如下:

            row = cursor.fetchone()

            # Check if there are rows to update XML
            if row:
                # Get the column names from cursor description
                column_names = [desc[0] for desc in cursor.description]

                # Create a dictionary to map column names to values (uppercase column names)
                row_data = dict(zip(map(str.upper, column_names), row))
                print(row_data)

                # Determine whether to update <sa> or <parent> elements based on tag
                if record.tag == 'sa':
                    xml_sql_mapping = xml_sql_mapping_sa
                elif record.tag == 'parent':
                    xml_sql_mapping = xml_sql_mapping_parent
                else:
                    print(f"Unsupported XML element tag: {record.tag}")
                    continue

                # Loop through the XML fields and update if necessary
                for xml_field, sql_column in xml_sql_mapping.items():
                    if sql_column in row_data:
                        sql_value = row_data[sql_column]
                        xml_element = record.find(xml_field)
                        print(f"The xml_field is {xml_field}.")
                        print(f"XML element is {xml_element}")

                        # Check if the XML element exists
                        if xml_element is not None:
                            # Check if the SQL value is not None and different from XML value
                            if sql_value is not None and xml_element.text != sql_value:
                                xml_element.text = sql_value
                        else:
                            print(f"XML element {xml_field} not found in the <{record.tag}> element.")

            else:
                print(f"No record found for student ID: {v_student_ID}")

        finally:
            # Close the cursor for each student
            cursor.close()

一切正常,只是找不到 xml 元素。这是我的打印报表的一部分。 (数据已更改)

SchoolSid: 020194486
{'RZECAST_KEYNCAAID': '151104', 'SPRIDEN_ID': '123456', 'BIRTHDATE': '2004-05-23', 'EMAIL': '[email protected]', 'ETHNICODE': '5', 'FIRSTNAME': 'A____', 'LASTNAME': 'A___', 'MI': None, 'GENDER': 'F', 'PREFERREDNAME': None, 'PRIMARY_PHONE': '406-555-5555'}
The xml_field is ncaaId.
XML element is None
XML element ncaaId not found in the <sa> element.
The xml_field is schoolSid.
XML element is None 

相关代码行是这样的: xml_element = record.find(xml_field)

xml_element 应使用 xml 中的值填充,但它什么也没找到。

python xml-parsing elementtree
1个回答
0
投票
The xml_field is ncaaId.
XML element is None
XML element ncaaId not found in the <sa> element.

ncaaId
不是元素。它是
sa
元素的属性。

所以你可能想做类似的事情(也许也重命名变量,因为你没有选择元素):

xml_element = record.get(xml_field)
© www.soinside.com 2019 - 2024. All rights reserved.