Mam plik pom że ma następującą definicją:Reading Maven Pom XML w Pythonie
<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>
i szukam zbudować słownika wyłączyć name
& value
elementów pod property
pod elementem mappings
.
Więc co usiłuję dowiedzieć się, jak uzyskać wszystkie możliwe mappings
elementy (Incase wielu profili Build), więc można uzyskać wszystkie elementy property
pod nim i od czytania o Supported XPath syntax następujące powinien wypisać wszystkie możliwe tekst/elementy wartości:
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)
Który niczego nie zwraca. Próbowałem po prostu drukowania wszystkie mappings
elementy, a otrzymasz:
>>> print root.findall('*/mappings')
[]
I kiedy wydrukować wszystko od root
uzyskać:
>>> 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>]
co sprawiło mi spróbować druku:
>>> print root.findall('*/{http://maven.apache.org/POM/4.0.0}mappings')
[]
Ale to też nie działa.
Wszelkie sugestie byłyby świetne.
Dzięki,
Znalazłem to sens, który pracuje dla mnie i sprawia, że nieco mniej gadatliwy: https://gist.github.com/kennedyj/1895332 – borism