在Python xml.dom中不是递归(单节点级别)getElementsByTagName

时间:2022-12-25 22:39:04

Is there any way to use getElementsByTagName only at a single node level and not recursively?

有没有办法只在单个节点级别使用getElementsByTagName而不是递归?

E.g. consider parsing a pom.xml file:

例如。考虑解析pom.xml文件:

<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">

    <parent>
        <groupId>com.parent</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>2.0.0</modelVersion>
    <groupId>com.parent.somemodule</groupId>
    <artifactId>some_module</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Some Module</name>
    ...

If I want to get groupId at the top level (specifically project->groupId, not project->parent->groupId), I use:

如果我想将groupId放在顶层(特别是project-> groupId,而不是project-> parent-> groupId),我使用:

xmldoc = minidom.parse('pom.xml')
groupId = xmldoc.getElementsByTagName("groupId")[0].childNodes[0].nodeValue

But unfortunately, that finds the first physical occurrence of groupId in the file regardless of the hierarchy level, which is project->parent->groupId. I actually want to do a unrecursive find ONLY at a specific node level, not within its children. Is there a way to do it in xml.dom?

但不幸的是,无论层次结构级别如何,它都会在文件中找到groupId的第一次物理出现,即project-> parent-> groupId。我实际上只想在特定节点级别进行非递归查找,而不是在其子级内。有没有办法在xml.dom中做到这一点?

UPDATE: I switched to BeautifulSoup but still having the same problem with implicit recursive traversing: Finding a nonrecursive DOM subnode in Python using BeautifulSoup

更新:我切换到BeautifulSoup但仍然存在隐式递归遍历的相同问题:使用BeautifulSoup在Python中查找非递归DOM子节点

1 个解决方案

#1


2  

You can iterate over getElementsByTagName() results and take the first element that is in on the root level:

您可以迭代getElementsByTagName()结果并获取根级别上的第一个元素:

group_id_element =  next(element for element in xmldoc.getElementsByTagName("groupId")
                         if element.parentNode == xmldoc.documentElement)

print group_id_element.childNodes[0].nodeValue

Note that it would be easier, shorter and faster to do the same with ElementTree, which is also a part of standard library.

请注意,使用ElementTree(也是标准库的一部分)执行相同操作会更容易,更短,更快。

Hope that helps.

希望有所帮助。

#1


2  

You can iterate over getElementsByTagName() results and take the first element that is in on the root level:

您可以迭代getElementsByTagName()结果并获取根级别上的第一个元素:

group_id_element =  next(element for element in xmldoc.getElementsByTagName("groupId")
                         if element.parentNode == xmldoc.documentElement)

print group_id_element.childNodes[0].nodeValue

Note that it would be easier, shorter and faster to do the same with ElementTree, which is also a part of standard library.

请注意,使用ElementTree(也是标准库的一部分)执行相同操作会更容易,更短,更快。

Hope that helps.

希望有所帮助。