2012-05-08 97 views
0

我如何知道父節點的名稱說我在標籤=「襯衫」,我如何知道它的父母是「FG」的父母是john_carter。難道是知道(在minidom命名)xml python解析獲取父節點名稱: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> 
             . 
             . 
             . 
          . 
          . 
          . 
       . 
       . 
       . 


    . 
    . 
    . 
+3

發佈示例XML時,如果它是有效的XML,它將很有幫助。 – MattH

回答

1

也許這樣嗎?

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

謝謝。是否有可能找到子節點。 FG到john_carter去襯衫。也就是說,FG的孩子是john_carter,而john_carter的孩子是襯衫 – nish

+0

基本上我只需要將3(其中有5個以上)關卡放入對象。有沒有辦法知道不同的水平 – nish

+0

等待一段時間...沒有機構應該回復這..我看到,除了上述的一點點將有所幫助。將很快發佈 – nish