0

我怎么知道父节点的名称说我在标签 =“衬衫”,我怎么知道它的父节点是 john_carter,其父节点是“FG”。有没有可能知道(在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>
                                        .
                                        .
                                        .
                            .
                            .
                            .
                .
                .
                .


    .
    .
    .
4

1 回答 1

3

也许像这样?

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"))
于 2012-05-08T15:18:10.903 回答