我有一个 pom 文件,其中定义了以下内容:
<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>4.0.0</modelVersion>
<groupId>org.welsh</groupId>
<artifactId>my-site</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<profiles>
    <profile>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.welsh.utils</groupId>
                    <artifactId>site-tool</artifactId>
                    <version>1.0</version>
                    <executions>
                        <execution>
                            <configuration>
                                <mappings>
                                    <property>
                                        <name>homepage</name>
                                        <value>/content/homepage</value>
                                    </property>
                                    <property>
                                        <name>assets</name>
                                        <value>/content/assets</value>
                                    </property>
                                </mappings>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
</project>
我正在寻找从元素下的 & 元素中name构建一个字典。valuepropertymappings
所以我试图弄清楚如何获取所有可能的mappings元素(在多个构建配置文件的情况下),以便我可以获取property它下的所有元素,并通过阅读支持的 XPath 语法,以下应该打印出所有可能的文本/值元素:
import xml.etree.ElementTree as xml
pomFile = xml.parse('pom.xml')
root = pomFile.getroot()
for mapping in root.findall('*/mappings'):
    for prop in mapping.findall('.//property'):
        logging.info(prop.find('name').text + " => " + prop.find('value').text)
什么都不返回。我试着打印出所有mappings元素并得到:
>>> print root.findall('*/mappings')
[]
当我打印出root我得到的所有内容时:
>>> print root.findall('*')
[<Element '{http://maven.apache.org/POM/4.0.0}modelVersion' at 0x10b38bd50>, <Element '{http://maven.apache.org/POM/4.0.0}groupId' at 0x10b38bd90>, <Element '{http://maven.apache.org/POM/4.0.0}artifactId' at 0x10b38bf10>, <Element '{http://maven.apache.org/POM/4.0.0}version' at 0x10b3900d0>, <Element '{http://maven.apache.org/POM/4.0.0}packaging' at 0x10b390110>, <Element '{http://maven.apache.org/POM/4.0.0}name' at 0x10b390150>, <Element '{http://maven.apache.org/POM/4.0.0}properties' at 0x10b390190>, <Element '{http://maven.apache.org/POM/4.0.0}build' at 0x10b390310>, <Element '{http://maven.apache.org/POM/4.0.0}profiles' at 0x10b390390>]
这让我尝试打印:
>>> print root.findall('*/{http://maven.apache.org/POM/4.0.0}mappings')
[]
但这也行不通。
任何建议都会很棒。
谢谢,