我有一个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
&amp; value
元素下的property
下的mappings
元素。
所以我想弄清楚如何获取所有可能的mappings
元素(包含多个构建配置文件),这样我就可以得到它下的所有property
个元素,并阅读{{3下面应该打印出所有可能的文本/值元素:
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')
[]
但那也不起作用。
任何建议都会很棒。
谢谢,
答案 0 :(得分:4)
好的,发现当我从project
元素中移除maven时,只有<project>
我可以这样做:
for mapping in root.findall('*//mappings'):
logging.info(mapping)
for prop in mapping.findall('./property'):
logging.info(prop.find('name').text + " => " + prop.find('value').text)
哪会导致:
INFO:root:<Element 'mappings' at 0x10d72d350>
INFO:root:homepage => /content/homepage
INFO:root:assets => /content/assets
但是,如果我将Maven的东西留在顶部,我可以这样做:
for mapping in root.findall('*//{http://maven.apache.org/POM/4.0.0}mappings'):
logging.info(mapping)
for prop in mapping.findall('./{http://maven.apache.org/POM/4.0.0}property'):
logging.info(prop.find('{http://maven.apache.org/POM/4.0.0}name').text + " => " + prop.find('{http://maven.apache.org/POM/4.0.0}value').text)
结果是:
INFO:root:<Element '{http://maven.apache.org/POM/4.0.0}mappings' at 0x10aa7f310>
INFO:root:homepage => /content/homepage
INFO:root:assets => /content/assets
然而,我很想知道如何避免因为它将我锁定为这种格式而不得不考虑maven的东西。
编辑:
好的,我设法得到了一些更冗长的东西:
import xml.etree.ElementTree as xml
def getMappingsNode(node, nodeName):
if node.findall('*'):
for n in node.findall('*'):
if nodeName in n.tag:
return n
else:
return getMappingsNode(n, nodeName)
def getMappings(rootNode):
mappingsNode = getMappingsNode(rootNode, 'mappings')
mapping = {}
for prop in mappingsNode.findall('*'):
key = ''
val = ''
for child in prop.findall('*'):
if 'name' in child.tag:
key = child.text
if 'value' in child.tag:
val = child.text
if val and key:
mapping[key] = val
return mapping
pomFile = xml.parse('pom.xml')
root = pomFile.getroot()
mappings = getMappings(root)
print mappings
答案 1 :(得分:3)
我用python修改了一个pom.xml。似乎 etree 没有很好地记录。花了一段时间才使一切工作,但它现在似乎有用了。
正如您在以下代码段中所看到的, Maven 使用命名空间http://maven.apache.org/POM/4.0.0
。根节点中的属性xmlns
定义默认命名空间。属性xmlns:xsi
也定义了命名空间,但它仅用于xsi:schemaLocation
。
<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">
要在profile
等方法中使用find
等标记,您还必须指定命名空间。例如,您可以编写以下内容来查找所有profile
- 标记。
import xml.etree as xml
pom = xml.parse('pom.xml')
for profile in pom.findall('//{http://maven.apache.org/POM/4.0.0}profile'):
print(repr(profile))
另一个重要的事情是//
。使用您的xml文件,*/
对于此示例将具有相同的结果。但它会 不适用于mappings
等其他标签。由于*
仅代表一个级别,*/child
可以扩展为parent/tag
或xyz/tag
,但不能扩展为xyz/parent/tag
。
我认为这是您上面代码中的主要问题。您必须使用//
*/
{}允许任何子元素而不是直接子元素。你必须指定命名空间。使用它,你应该能够做这样的事情来找到所有的映射:
pom = xml.parse('pom.xml')
map = {}
for mapping in pom.findall('//{http://maven.apache.org/POM/4.0.0}mappings'
'/{http://maven.apache.org/POM/4.0.0}property'):
name = mapping.find('{http://maven.apache.org/POM/4.0.0}name').text
value = mapping.find('{http://maven.apache.org/POM/4.0.0}value').text
map[name] = value
但是指定上面的命名空间并不是很好。您可以定义命名空间映射并将其作为find
和findall
的第二个参数:
# ...
nsmap = {'m': 'http://maven.apache.org/POM/4.0.0'}
for mapping in pom.findall('//m:mappings/m:property', nsmap):
name = mapping.find('m:name', nsmap).text
value = mapping.find('m:value', nsmap).text
map[name] = value