Python利用命名空間解析XML文檔
問(wèn)題
你想解析某個(gè)XML文檔,文檔中使用了XML命名空間。
解決方案
考慮下面這個(gè)使用了命名空間的文檔:
<?xml version="1.0" encoding="utf-8"?>
<top>
<author>David Beazley</author>
<content>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
</content>
</top>
如果你解析這個(gè)文檔并執(zhí)行普通的查詢,你會(huì)發(fā)現(xiàn)這個(gè)并不是那么容易,因?yàn)樗胁襟E都變得相當(dāng)?shù)姆爆崱?/p>
>>> # Some queries that work
>>> doc.findtext('author')
'David Beazley'
>>> doc.find('content')
<Element 'content' at 0x100776ec0>
>>> # A query involving a namespace (doesn't work)
>>> doc.find('content/html')
>>> # Works if fully qualified
>>> doc.find('content/{http://www.w3.org/1999/xhtml}html')
<Element '{http://www.w3.org/1999/xhtml}html' at 0x1007767e0>
>>> # Doesn't work
>>> doc.findtext('content/{http://www.w3.org/1999/xhtml}html/head/title')
>>> # Fully qualified
>>> doc.findtext('content/{http://www.w3.org/1999/xhtml}html/'
... '{http://www.w3.org/1999/xhtml}head/{http://www.w3.org/1999/xhtml}title')
'Hello World'
>>>
你可以通過(guò)將命名空間處理邏輯包裝為一個(gè)工具類來(lái)簡(jiǎn)化這個(gè)過(guò)程:
class XMLNamespaces:
def __init__(self, **kwargs):
self.namespaces = {}
for name, uri in kwargs.items():
self.register(name, uri)
def register(self, name, uri):
self.namespaces[name] = '{'+uri+'}'
def __call__(self, path):
return path.format_map(self.namespaces)
通過(guò)下面的方式使用這個(gè)類:
>>> ns = XMLNamespaces(html='http://www.w3.org/1999/xhtml')
>>> doc.find(ns('content/{html}html'))
<Element '{http://www.w3.org/1999/xhtml}html' at 0x1007767e0>
>>> doc.findtext(ns('content/{html}html/{html}head/{html}title'))
'Hello World'
>>>
討論
解析含有命名空間的XML文檔會(huì)比較繁瑣。 上面的 XMLNamespaces 僅僅是允許你使用縮略名代替完整的URI將其變得稍微簡(jiǎn)潔一點(diǎn)。
很不幸的是,在基本的 ElementTree 解析中沒(méi)有任何途徑獲取命名空間的信息。 但是,如果你使用 iterparse() 函數(shù)的話就可以獲取更多關(guān)于命名空間處理范圍的信息。例如:
>>> from xml.etree.ElementTree import iterparse
>>> for evt, elem in iterparse('ns2.xml', ('end', 'start-ns', 'end-ns')):
... print(evt, elem)
...
end <Element 'author' at 0x10110de10>
start-ns ('', 'http://www.w3.org/1999/xhtml')
end <Element '{http://www.w3.org/1999/xhtml}title' at 0x1011131b0>
end <Element '{http://www.w3.org/1999/xhtml}head' at 0x1011130a8>
end <Element '{http://www.w3.org/1999/xhtml}h1' at 0x101113310>
end <Element '{http://www.w3.org/1999/xhtml}body' at 0x101113260>
end <Element '{http://www.w3.org/1999/xhtml}html' at 0x10110df70>
end-ns None
end <Element 'content' at 0x10110de68>
end <Element 'top' at 0x10110dd60>
>>> elem # This is the topmost element
<Element 'top' at 0x10110dd60>
>>>
最后一點(diǎn),如果你要處理的XML文本除了要使用到其他高級(jí)XML特性外,還要使用到命名空間, 建議你最好是使用 lxml 函數(shù)庫(kù)來(lái)代替 ElementTree 。 例如,lxml 對(duì)利用DTD驗(yàn)證文檔、更好的XPath支持和一些其他高級(jí)XML特性等都提供了更好的支持。 這一小節(jié)其實(shí)只是教你如何讓XML解析稍微簡(jiǎn)單一點(diǎn)。
以上就是Python利用命名空間解析XML文檔的詳細(xì)內(nèi)容,更多關(guān)于Python命名空間解析XML文檔的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
tensor和numpy的互相轉(zhuǎn)換的實(shí)現(xiàn)示例
這篇文章主要介紹了tensor和numpy的互相轉(zhuǎn)換的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
通過(guò)python 執(zhí)行 nohup 不生效的解決
這篇文章主要介紹了通過(guò)python 執(zhí)行 nohup 不生效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
python實(shí)現(xiàn)RGB與YCBCR顏色空間轉(zhuǎn)換
這篇文章主要介紹了python實(shí)現(xiàn)RGB與YCBCR顏色空間轉(zhuǎn)換,RGB與YCbCr顏色空間概念的與變換關(guān)系,包括內(nèi)容灰度值和亮度的關(guān)系、RGB顏色空間與顏色控制、YCbCr顏色空間及與RGB的變換關(guān)系,需要的小伙伴可以參考一下2022-03-03

