首页 > 解决方案 > 如何在 Python 3.7.3 中解析没有命名空间的 xml-string(不是文件)?

问题描述

我正在将字符串转换为 xml。如何解析没有命名空间的 XML?也许您可以建议其他库使用 XML 字符串?

这是我的代码:

import xml.etree.ElementTree as ET
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
country_data_as_string = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:aa="http://vio.pfr.ru/Statements/ApplyApplication/1.0"><soap:Header><wsa:MessageID>12fd0e5b78cf44c2a61a1c6fc238cc51</wsa:MessageID><wsa:Action>http://vio.pfr.ru/person/Application/MSKApplicationPortType/ApplyApplicationRequest</wsa:Action><GlobalProcessID>66826749770843f4bfc5f359a86165af</GlobalProcessID></soap:Header></soap:Envelope>' 
doc = ET.fromstring(country_data_as_string)  

      
for elem in doc:                    
    for i in elem:                  
        print(i)                    
        if i.tag == 'MessageID':    
            print(i)         

   

标签: python-3.xxmlxml-parsing

解决方案


以下代码将允许您在不使用命名空间模式的情况下读取整个xml :

import xml.etree.ElementTree as ET
country_data_as_string = r"""<?xml version="1.0" encoding="utf-8"?>
                                <soap:Envelope 
                                xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
                                xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" 
                                xmlns:aa="http://vio.pfr.ru/Statements/ApplyApplication/1.0">
                                    <soap:Header>
                                        <wsa:MessageID>12fd0e5b78cf44c2a61a1c6fc238cc51</wsa:MessageID>
                                        <wsa:Action>http://vio.pfr.ru/person/Application/MSKApplicationPortType/ApplyApplicationRequest</wsa:Action>
                                        <GlobalProcessID>66826749770843f4bfc5f359a86165af</GlobalProcessID>
                                    </soap:Header>
                                </soap:Envelope>
                        """
doc = ET.fromstring(country_data_as_string)  
for node in doc.iter():
    print(node.tag+': ', node.text)   # node.text has the data encompassed by the tags

上面的输出:

{http://www.w3.org/2003/05/soap-envelope}Envelope:  
                                    
{http://www.w3.org/2003/05/soap-envelope}Header:  
                                        
{http://schemas.xmlsoap.org/ws/2004/08/addressing}MessageID:  12fd0e5b78cf44c2a61a1c6fc238cc51
{http://schemas.xmlsoap.org/ws/2004/08/addressing}Action:  http://vio.pfr.ru/person/Application/MSKApplicationPortType/ApplyApplicationRequest
GlobalProcessID:  66826749770843f4bfc5f359a86165af

推荐阅读