簡體   English   中英

使用 Python 從 XML 文件中刪除不需要的元素

[英]Use Python to remove unneeded elements from XML file

我正在 Python 中編寫一個程序來使用 API,它似乎不會根據用戶是否被視為活躍來過濾掉請求。 當我向 API 詢問活躍用戶列表時,我得到了一個更長的 XML 文檔,看起來像下面的文本,它仍然包括<active>標簽為 false 的用戶。

<ArrayOfuser xmlns="WebsiteWhereDataComesFrom.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <user>
        <active>false</active>
        <datelastlogin>2/3/2014 10:21:13 PM</datelastlogin>
        <dept>0</dept>
        <email/>
        <firstname>userfirstname</firstname>
        <lastname>userlastname</lastname>
        <lastupdated/>
        <lastupdatedby/>
        <loginemail>userloginemail</loginemail>
        <phone1/>
        <phone2/>
        <rep>userinitials</rep>
    </user>
    <user>
        <active>true</active>
        <datelastlogin>8/21/2019 9:16:30 PM</datelastlogin>
        <dept>3</dept>
        <email>useremail</email>
        <firstname>userfirstname</firstname>
        <lastname>userlastname</lastname>
        <lastupdated>2/6/2019 11:10:29 PM</lastupdated>
        <lastupdatedby>userinitials</lastupdatedby>
        <loginemail>userloginemail</loginemail>
        <phone1>userphone</phone1>
        <phone2/>
        <rep>userinitials</rep>
    </user>
</ArrayOfuser>

該程序需要最終僅從活動用戶返回<rep>標記的列表。

這是我作為這個項目的開始嘗試的代碼。 我可能過於復雜了,因為我試圖為活躍用戶解析 users.xml 然后保存一個包含所有關於活躍用戶的 XML 數據的文件,然后在該文件中使用 for 循環從每個<rep>標簽獲取信息並保存它到一個列表:

to_remove = ['<active>false</active>']
with open('users.xml') as xmlfile, open('activeusers.xml','w') as newfile:
    for line in xmlfile:
        if not any(remo in line for remo in to_remove):
            newfile.write(line)

在 activeusers.xml 中,我期待看到下面的代碼塊。

<ArrayOfuser xmlns="WebsiteWhereDataComesFrom.com" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <user>
        <active>true</active>
        <datelastlogin>8/21/2019 9:16:30 PM</datelastlogin>
        <dept>3</dept>
        <email>useremail</email>
        <firstname>userfirstname</firstname>
        <lastname>userlastname</lastname>
        <lastupdated>2/6/2019 11:10:29 PM</lastupdated>
        <lastupdatedby>userinitials</lastupdatedby>
        <loginemail>userloginemail</loginemail>
        <phone1>userphone</phone1>
        <phone2/>
        <rep>userinitials</rep>
    </user>
</ArrayOfuser>

結果是用戶 xml 文件的相同副本。 我的猜測是,如果程序正在復制所有內容,它必須正確讀取文件,但它絕對不會刪除我需要的內容,因此語法一定不正確。 這只是我想到的解決方案,程序不必創建一個名為 activeusers.xml 的新文件。 最終目標是只為活躍用戶獲取<rep>標簽列表,所以如果有更好的方法,我很想知道,因為我是 XML 的新手和 Python 的新手。

由於您正在處理 xml,因此您應該使用適當的 xml 解析器。 請注意,在這種情況下,您還必須處理名稱空間。

所以試試這個:

from lxml import etree
#load your file
doc = etree.parse("users.xml")
#declare namespaces
ns = {'xx': 'WebsiteWhereDataComesFrom.com'}

#locate your deletion targets
targets = doc.xpath('//xx:user[xx:active="false"]',namespaces=ns)
for target in targets:
    target.getparent().remove(target)

#save your file
with open("newfile.xml", 'a') as file:
    file.write(etree.tostring(doc).decode())

這應該有您預期的 output。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM