tập tin XML này được đặt tên example.xml
:Python: ElementTree, có được chuỗi namespace của một Element
<?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>
Nếu tôi tải example.xml và phân tích nó với ElementTree tôi có thể nhìn thấy không gian tên của nó là 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>
tôi đã không tìm thấy một phương pháp để gọi điện để nhận được chỉ là không gian tên từ một Element
mà không cần đến phân tích cú pháp str(an_element)
của một Element. Có vẻ như phải có một cách tốt hơn.
bạn có biết cách sử dụng phương pháp tìm trong trường hợp này không? nó không hoạt động ở đây ... – caarlos0