這個XML文件被命名爲example.xml
的:的Python:ElementTree中,獲得命名空間的字符串的元素
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>14.0.0</modelVersion>
<groupId>.com.foobar.flubber</groupId>
<artifactId>uberportalconf</artifactId>
<version>13-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Environment for UberPortalConf</name>
<description>This is the description</description>
<properties>
<birduberportal.version>11</birduberportal.version>
<promotiondevice.version>9</promotiondevice.version>
<foobarportal.version>6</foobarportal.version>
<eventuberdevice.version>2</eventuberdevice.version>
</properties>
<!-- A lot more here, but as it is irrelevant for the problem I have removed it -->
</project>
如果我加載的example.xml與ElementTree的解析它,我可以看到它的命名空間爲http://maven.apache.org/POM/4.0.0
。
>>> from xml.etree import ElementTree
>>> tree = ElementTree.parse('example.xml')
>>> print tree.getroot()
<Element '{http://maven.apache.org/POM/4.0.0}project' at 0x26ee0f0>
我還沒有找到一個方法來調用得到的只是來自Element
命名空間而不是訴諸解析元素的str(an_element)
。似乎有一個更好的方法。
你知道如何在這種情況下使用find方法嗎?它沒有在這裏工作... – caarlos0 2012-05-05 03:48:26