在python中使用内联模式读取XML

2024-10-01 04:44:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有多个具有内联模式的XML文件。我尝试使用python解析xml数据,但它确实给出了任何结果。在

我想得到元素的值

奥格丽德 角色

从所有ogridroles标记

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified">
        <xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd"/>
        <xsd:element name="ogridroles">
            <xsd:complexType>
                <xsd:sequence>
                    <xsd:element name="ogrid_cde" type="sqltypes:int" nillable="1"/>
                    <xsd:element name="role" nillable="1">
                        <xsd:simpleType>
                            <xsd:restriction base="sqltypes:char" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
                                <xsd:maxLength value="1"/>
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:element>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
    </xsd:schema>
    <ogridroles xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <ogrid_cde>28</ogrid_cde>
        <role>T</role>
    </ogridroles>
    <ogridroles xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <ogrid_cde>75</ogrid_cde>
        <role>T</role>
    </ogridroles>
    <ogridroles xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <ogrid_cde>93</ogrid_cde>
        <role>O</role>
    </ogridroles>
    <ogridroles xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
        <ogrid_cde>135</ogrid_cde>
        <role>O</role>
    </ogridroles>
</root>

Python代码

^{pr2}$

Tags: comhttpsqlelementschemasmicrosoftrolexsd
3条回答

对于大文件,请使用lxml.etree.iterparse()与zipfile模块结合使用,可直接从压缩文件中读取。这将返回一个必须显式打开的迭代器,以防止整个文件被读入内存。在

from lxml import etree
from zipfile import ZipFile

zipped_file = ZipFile(<your file>, 'r')
for event, element in etree.iterparse(zipped_file.open(<filename inside zip>)):
    for item in [ogrid_cde, role]:
        if item in element.tag:
            print('{}: {}'.format(item, element.text))

它应该打印出成对的字段名(与元素标记匹配的项)和值(从元素检索到的文本)。在

这个代码有效。在

import xml.etree.ElementTree as ET

tree = ET.parse('ogridroles.xml')
root = tree.getroot()

for child in root:
    print(child[0].text, "==", child[1].text)

感谢Laughing Vergil提供的线索

除非我弄错了,否则xmlns值将作为标记返回,而不是作为元素。试着检查标签。在

相关问题 更多 >