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

在 Python 中使用 ElementTree 解析帶有命名空間的

Parsing XML with namespaces using ElementTree in Python(在 Python 中使用 ElementTree 解析帶有命名空間的 XML)
本文介紹了在 Python 中使用 ElementTree 解析帶有命名空間的 XML的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個xml,它的一小部分看起來像這樣:

I have an xml, small part of it looks like this:

<?xml version="1.0" ?>
<i:insert xmlns:i="urn:com:xml:insert" xmlns="urn:com:xml:data">
  <data>
    <image imageId="1"></image>
    <content>Content</content>
  </data>
</i:insert>

當(dāng)我使用 ElementTree 解析它并將其保存到一個文件中時,我看到以下內(nèi)容:

When i parse it using ElementTree and save it to a file i see following:

<ns0:insert xmlns:ns0="urn:com:xml:insert" xmlns:ns1="urn:com:xml:data">
  <ns1:data>
    <ns1:image imageId="1"></ns1:image>
    <ns1:content>Content</ns1:content>
  </ns1:data>
</ns0:insert>

為什么它會改變前綴并將它們放在任何地方?使用 minidom 我沒有這樣的問題.配置好了嗎?ElementTree 的文檔很差.問題是,在這樣的解析之后我找不到任何節(jié)點,例如 image - 如果我像 {namespace}image 或只是 image.為什么?任何建議都非常感謝.

Why does it change prefixes and put them everywhere? Using minidom i don't have such problem. Is it configured? Documentation for ElementTree is very poor. The problem is, that i can't find any node after such parsing, for example image - can't find it with or without namespace if i use it like {namespace}image or just image. Why's that? Any suggestions are strongly appreciated.

我已經(jīng)嘗試過的:

import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
for a in root.findall('ns1:image'):
    print a.attrib

這會返回一個錯誤,而另一個則什么也不返回:

This returns an error and the other one returns nothing:

for a in root.findall('{urn:com:xml:data}image'):
    print a.attrib

我也嘗試過制作這樣的命名空間并使用它:

I also tried to make namespace like this and use it:

namespaces = {'ns1': 'urn:com:xml:data'}
for a in root.findall('ns1:image', namespaces):
    print a.attrib

它什么也不返回.我做錯了什么?

It returns nothing. What am i doing wrong?

推薦答案

這個片段來自你的問題,

This snippet from your question,

for a in root.findall('{urn:com:xml:data}image'):
    print a.attrib

不輸出任何內(nèi)容,因為它只查找樹根的直接 {urn:com:xml:data}image 子級.

does not output anything because it only looks for direct {urn:com:xml:data}image children of the root of the tree.

這個稍加修改的代碼,

for a in root.findall('.//{urn:com:xml:data}image'):
    print a.attrib

將打印 {'imageId': '1'} 因為它使用 .//,它會選擇所有級別的匹配子元素.

will print {'imageId': '1'} because it uses .//, which selects matching subelements on all levels.

參考:https://docs.python.org/2/library/xml.etree.elementtree.html#supported-xpath-syntax.

ElementTree 默認(rèn)情況下不僅保留原始命名空間前綴有點煩人,但請記住,無論如何,前綴并不重要.register_namespace() 函數(shù)可用于在序列化 XML 時設(shè)置所需的前綴.該函數(shù)對解析或搜索沒有任何影響.

It is a bit annoying that ElementTree does not just retain the original namespace prefixes by default, but keep in mind that it is not the prefixes that matter anyway. The register_namespace() function can be used to set the wanted prefix when serializing the XML. The function does not have any effect on parsing or searching.

這篇關(guān)于在 Python 中使用 ElementTree 解析帶有命名空間的 XML的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現(xiàn)問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲在變量中?)
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 對象)
主站蜘蛛池模板: 亚洲aⅴ| 国产一区在线免费 | 国产区在线免费观看 | 欧美视频 | 成人毛片视频免费 | 成人婷婷 | 我想看国产一级毛片 | 久久久久se | 精品欧美一区二区三区久久久 | 91免费观看视频 | 国产精品1区2区3区 男女啪啪高潮无遮挡免费动态 | 一区二区三区四区在线 | 国产 日韩 欧美 在线 | 亚洲一区中文字幕 | 龙珠z国语版在线观看 | 国产一区二区精 | 精品在线一区二区三区 | 成人av一区 | 色偷偷888欧美精品久久久 | 国产不卡在线 | 久久久久久久一区二区 | 欧美日韩亚洲二区 | 国产成人高清视频 | 久在线精品视频 | 一本一道久久a久久精品综合 | 国产成人精品区一区二区不卡 | 色妹子综合网 | 欧美炮房| 久久久一| 久久久精品网站 | 欧美性video 精品亚洲一区二区 | 亚洲精品一区久久久久久 | 91不卡 | 成人黄色电影在线播放 | 91在线精品视频 | 免费看91 | 亚洲视频在线免费 | 中文字幕人成人 | 人人干人人艹 | 国产999精品久久久影片官网 | 草久久|