2015-04-01 60 views

回答

1

您需要手柄namespaces(和empty one太):

namespaces = { 
    "dc":"http://purl.org/dc/elements/1.1/", 
    "cc": "http://creativecommons.org/ns#", 
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", 
    "svg": "http://www.w3.org/2000/svg", 
    "myns": "http://www.w3.org/2000/svg" 
} 

tree = ET.fromstring(data) 
for rect in tree.xpath("//myns:rect", namespaces=namespaces): 
    print rect.attrib.get("id") 

其中data为您提供的XML字符串。

出于测试目的,它只是打印rect元素id属性:

rect3347 
rect3349 
rect3351 
rect3351-1 
rect3351-17 
rect3351-1-4 
rect3397 
rect3399 
rect3401 
rect3403