2015-01-05 61 views
0

我正在使用xml.etree.ElementTree來解析XML文件。我想知道是否有一種方法來設置一個默認的ns用於所有的解析操作。Python的XML解析和默認NS

目前我使用的方法類似這樣的片段和它的確定:

name_space = 'myns' 
if node.tag == '{%s}Name' % name_space: 

有當ns是對所有標籤一樣什麼更好的辦法?

感謝

回答

0
NS_MAP = {"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", 
     "rdfs": "http://www.w3.org/2000/01/rdf-schema#", 
     "art": "http://simile.mit.edu/2003/10/ontologies/artstor#", 
     "foaf": "http://xmlns.com/foaf/0.1/", 
     "dcterms": "http://purl.org/dc/terms/", 
     "sioc": "http://rdfs.org/sioc/ns#", 
     "oa": "http://www.openannotation.org/ns/"} 
#short hands the ns 
RDF = "{%s}" % NS_MAP['rdf'] 
RDFS = "{%s}" % NS_MAP['rdfs'] 
ART = "{%s}" % NS_MAP['art'] 
FOAF = "{%s}" % NS_MAP['foaf'] 
DCTERMS = "{%s}" % NS_MAP['dcterms'] 
SIOC = "{%s}" % NS_MAP['sioc'] 
OA = "{%s}" % NS_MAP['oa'] 

,你可以嘗試這樣的事情