Ho un file pom che ha il seguente definito:lettura Maven Pom XML in Python
<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>
e sto cercando di costruire un dizionario largo delle name
& value
elementi sotto property
sotto l'elemento mappings
.
Quindi quello che sto cercando di capire come ottenere tutti i possibili elementi mappings
(Incase di più profili di compilazione) in modo da poter ottenere tutti gli elementi property
sotto di essa e da leggere su Supported XPath syntax il seguente dovrebbe stampare tutto il testo possibili/elementi di valore:
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)
Che non restituisce nulla. Ho provato solo la stampa di tutte le mappings
elementi e ottenere:
>>> print root.findall('*/mappings')
[]
E quando stampo il tutto da root
ottengo:
>>> 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>]
che mi ha fatto provare a stampare:
>>> print root.findall('*/{http://maven.apache.org/POM/4.0.0}mappings')
[]
Ma anche questo non funziona.
Qualsiasi suggerimento sarebbe ottimo.
Grazie,
ho trovato questa sostanza che lavora per me e lo rende un po 'meno prolissa: https://gist.github.com/kennedyj/1895332 – borism