用etree解析python中的XML

2024-05-29 00:15:09 发布

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

我得到了一个XML文件:

<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>antigon</name>
    <SSIDConfig>
        <SSID>
            <name>antigon</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>THIS IS WHAT I WANNA GET</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
    <MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
        <enableRandomization>false</enableRandomization>
    </MacRandomization>
</WLANProfile>

我知道HTML/XML的逻辑,但我找不到如何使用Etree和python获得“这就是我想要的”,这两个版本都在上一版本中。有人能帮我吗?非常感谢


Tags: namecomfalsehttpwwwxmlprofilemicrosoft
2条回答

下面

import xml.etree.ElementTree as ET
import re

xml = '''<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>antigon</name>
    <SSIDConfig>
        <SSID>
            <name>antigon</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>THIS IS WHAT I WANNA GET</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
    <MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
        <enableRandomization>false</enableRandomization>
    </MacRandomization>
</WLANProfile>'''
xml = re.sub(' xmlns="[^"]+"', '', xml, count=1)
root = ET.fromstring(xml)
key_material = root.find('.//keyMaterial')
print(key_material.text)

输出

THIS IS WHAT I WANNA GET

See the docs(和this answer)了解有关使用ElementTree处理名称空间的详细信息

下面是一个例子:

import xml.etree.ElementTree as ET

ns_map = {"wlan": "http://www.microsoft.com/networking/WLAN/profile/v1"}

tree = ET.parse("input.xml")

print(tree.find(".//wlan:keyMaterial", namespaces=ns_map).text)

印刷品

THIS IS WHAT I WANNA GET

如果要修改该值并将其保存到文件中,请尝试以下操作:

import xml.etree.ElementTree as ET

ns_map = {"wlan": "http://www.microsoft.com/networking/WLAN/profile/v1"}

# Need this to make sure a prefix isn't added to your namespace declaration.
ET.register_namespace("", ns_map.get("wlan"))

tree = ET.parse("input.xml")

try:
    tree.find(".//wlan:keyMaterial", namespaces=ns_map).text = "NEW VALUE!"
except AttributeError:
    print("Unable to modify the keyMaterial value.")

tree.write("output.xml", xml_declaration=True, encoding="utf-8")

输出(Output.xml)

<?xml version='1.0' encoding='utf-8'?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1" xmlns:ns1="http://www.microsoft.com/networking/WLAN/profile/v3">
    <name>antigon</name>
    <SSIDConfig>
        <SSID>
            <name>antigon</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>NEW VALUE!</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
    <ns1:MacRandomization>
        <ns1:enableRandomization>false</ns1:enableRandomization>
    </ns1:MacRandomization>
</WLANProfile>

注意:ElementTree不能很好地处理多个默认名称空间,所以这就是为什么在输出中添加“ns1”前缀的原因

相关问题 更多 >

    热门问题