久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Python 中通過(guò) ElementTree 解析 xml 時(shí)如何保留命名

How to preserve namespaces when parsing xml via ElementTree in Python(在 Python 中通過(guò) ElementTree 解析 xml 時(shí)如何保留命名空間)
本文介紹了在 Python 中通過(guò) ElementTree 解析 xml 時(shí)如何保留命名空間的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

假設(shè)我有以下想要使用 Python 的 ElementTree 修改的 XML:

Assume that I've the following XML which I want to modify using Python's ElementTree:

<root xmlns:prefix="URI">
  <child company:name="***"/>
  ...
</root> 

我正在對(duì) XML 文件進(jìn)行一些修改,如下所示:

I'm doing some modification on the XML file like this:

import xml.etree.ElementTree as ET
tree = ET.parse('filename.xml')
# XML modification here
# save the modifications
tree.write('filename.xml')

那么 XML 文件看起來(lái)像:

Then the XML file looks like:

<root xmlns:ns0="URI">
  <child ns0:name="***"/>
  ...
</root>

如您所見,namepsace prefix 更改為 ns0.我知道使用 ET.register_namespace() 提到 這里.

As you can see, the namepsace prefix changed to ns0. I'm aware of using ET.register_namespace() as mentioned here.

ET.register_namespace() 的問(wèn)題在于:

  1. 你需要知道prefixURI
  2. 它不能與默認(rèn)命名空間一起使用.

例如如果 xml 看起來(lái)像:

e.g. If the xml looks like:

<root xmlns="http://uri">
    <child name="name">
    ...
    </child>
</root>

它將被轉(zhuǎn)換為:

<ns0:root xmlns:ns0="http://uri">
    <ns0:child name="name">
    ...
    </ns0:child>
</ns0:root>

如您所見,默認(rèn)命名空間更改為ns0.

As you can see, the default namespace is changed to ns0.

有沒(méi)有辦法用ElementTree解決這個(gè)問(wèn)題?

Is there any way to solve this problem with ElementTree?

推薦答案

ElementTree 將替換那些未使用 ET.register_namespace 注冊(cè)的命名空間前綴.要保留命名空間前綴,您需要先注冊(cè)它,然后再將修改寫入文件.以下方法完成這項(xiàng)工作并在全局范圍內(nèi)注冊(cè)所有命名空間,

ElementTree will replace those namespaces' prefixes that are not registered with ET.register_namespace. To preserve a namespace prefix, you need to register it first before writing your modifications on a file. The following method does the job and registers all namespaces globally,

def register_all_namespaces(filename):
    namespaces = dict([node for _, node in ET.iterparse(filename, events=['start-ns'])])
    for ns in namespaces:
        ET.register_namespace(ns, namespaces[ns])

這個(gè)方法應(yīng)該在ET.parse方法之前調(diào)用,這樣命名空間將保持不變,

This method should be called before ET.parse method, so that the namespaces will remain as unchanged,

import xml.etree.ElementTree as ET
register_all_namespaces('filename.xml')
tree = ET.parse('filename.xml')
# XML modification here
# save the modifications
tree.write('filename.xml')

這篇關(guān)于在 Python 中通過(guò) ElementTree 解析 xml 時(shí)如何保留命名空間的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時(shí)出現(xiàn)問(wèn)題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點(diǎn))
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲(chǔ)在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對(duì)象)
主站蜘蛛池模板: 免费精品久久久久久中文字幕 | 日韩免费视频一区二区 | 久久久久久高清 | 中文字幕日韩一区 | 一区二区三区精品视频 | 亚洲欧美日韩成人在线 | 日韩成人在线观看 | 91精品国产综合久久婷婷香蕉 | 国产一区二区在线免费播放 | 久久99国产精一区二区三区 | 国产精品麻 | 天天干夜夜操 | 亚洲一区二区三区在线 | 国产韩国精品一区二区三区 | 精品成人av | 精品一二三区在线观看 | 看a级黄色毛片 | 亚洲精品一区二区网址 | 国产精彩视频 | 91精品国产综合久久精品图片 | 亚洲有码转帖 | 免费观看的黄色网址 | 国产高清精品一区二区三区 | 成人不卡一区二区 | 午夜爽爽爽男女免费观看影院 | 色橹橹欧美在线观看视频高清 | 亚洲444kkkk在线观看最新 | 欧美伦理一区 | 国产一级网站 | 视频一区中文字幕 | 精品国产一区二区三区在线观看 | 伊人超碰在线 | 日韩一级一区 | 亚洲欧美中文日韩在线v日本 | 免费观看www| 亚洲国产91 | 人人爽日日躁夜夜躁尤物 | 91高清在线视频 | 国产精品久久久久久久久久久免费看 | 我要看黄色录像一级片 | 一级片网站视频 |