简体   繁体   English

xml python解析获取父节点名称:minidom

[英]xml python parsing get parent node name : minidom

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". 我怎么知道父节点的名字说我在label =“ shirt”,我怎么知道它的父节点是john_carter,其父节点是“ FG”。 Is it possible to know ( in minidom ) 是否可以知道(极少数)

-90 . -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>
                                        .
                                        .
                                        .
                            .
                            .
                            .
                .
                .
                .


    .
    .
    .

Maybe like this? 也许是这样吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM