本文介绍了xml python 解析获取父节点名称:minidom的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么知道父节点的名字说我在 label = "shirt" ,我怎么知道它的父节点是 john_carter ,它的父节点是 "FG".是否有可能知道(至少)

how do i know a parent node's name say i am at label = "shirt", how do i know that its parent is john_carter whose parent is "FG". Is it possible to know ( in minidom )

-90...

        <Object type="Layer" id="6" label="FG" expanded="True">
            <Properties>
                <Property id="blur" constant="True">
                    <Value>0</Value>
                </Property>
                .
                .
                .


                <Property id="objects" expanded="True" constant="True">
                    <Object type="Layer" id="7" label="john_carter">
                        <Properties>
                            <Property id="blur" constant="True">
                                <Value>0</Value>
                            </Property>
                            .
                            .
                            .


                            <Property id="objects" expanded="True" constant="True">
                                <Object type="Layer" id="8" label="shirt" selected="True">
                                    <Properties>
                                        <Property id="blur" constant="True">
                                            <Value>0</Value>
                                        </Property>
                                        .
                                        .
                                        .
                            .
                            .
                            .
                .
                .
                .


    .
    .
    .

推荐答案

也许是这样?

import xml.dom.minidom

def getParentObjectNode(node):
    while node.parentNode:
        node = node.parentNode
        if node.nodeName == "Object":
            return node

xml = xml.dom.minidom.parse("C:\\myxml.xml")
for shirtNode in xml.getElementsByTagName("Object"):
    if shirtNode.getAttribute("label") == "shirt":
        break

shirtParentObject = getParentObjectNode(shirtNode)
print(shirtParentObject.getAttribute("label"))
shirtParentParentObject = getParentObjectNode(shirtParentObject)
print(shirtParentParentObject.getAttribute("label"))

这篇关于xml python 解析获取父节点名称:minidom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:36