如何以正确的顺序遍历XML树(python)

时间:2016-08-24 16:34:48

标签: python xml traversal

我的树是这样的(第一个数字是级别,第二个数字是该级别的索引):

11  
12  
13  
  21  
  22  
    31
    32   
    33

当我遍历树时,我得到了这个:

----11
----12
------21
--------31
--------32
--------33
------22
----13

以下是代码:

from xml.etree import ElementTree

def perf_func(elem, func, level=0):
    func(elem,level)
    for child in elem.getchildren():
        perf_func(child, func, level+1)

def print_level(elem,level):
        print '-'*level+elem.tag

with open('Map.xml', 'rt') as f:
tree = ElementTree.parse(f)
perf_func(Tree, print_level)

更新:这是我正在使用的读取数据。我试图只打印文本(PlainText不为null)和此文本的格式

<ap:OneTopic>
    <ap:Topic Dirty="0000000000000001" OId="PktS/h+L5EeSqM/4hMH9JA==" Gen="0000000000000000">
        <ap:SubTopics>
            <ap:Topic Dirty="0000000000000001" OId="Tyvwk/tRCUGYSZe8Gm1hKw==" Gen="0000000000000000">
                <ap:SubTopics>
                    <ap:Topic Dirty="0000000000000001" OId="g1+iDTazMUaJf6HWSIz69Q==" Gen="0000000000000000">
                        <ap:SubTopics>
                            <ap:Topic Dirty="0000000000000001" OId="KTsoZbCe/Uq0IgYi9JVRvQ==" Gen="0000000000000000">
                                <ap:TopicViewGroup ViewIndex="0"/>
                                <ap:Text Dirty="0000000000000001" PlainText="1123" ReadOnly="false">
                                    <ap:Font/>
                                </ap:Text>
                            </ap:Topic>
                        </ap:SubTopics>
                        <ap:TopicViewGroup ViewIndex="0">
                            <ap:Collapsed Collapsed="false" Dirty="0000000000000001"/>
                        </ap:TopicViewGroup>
                        <ap:Text Dirty="0000000000000001" PlainText="1121" ReadOnly="false">
                            <ap:Font/>
                        </ap:Text>
                    </ap:Topic>
                    <ap:Topic Dirty="0000000000000001" OId="QzZ2AAYYS0a5aE+v2z9dkA==" Gen="0000000000000000">
                        <ap:TopicViewGroup ViewIndex="0"/>
                        <ap:Text Dirty="0000000000000001" PlainText="1122" ReadOnly="false">
                            <ap:Font/>
                        </ap:Text>
                    </ap:Topic>
                </ap:SubTopics>
                <ap:TopicViewGroup ViewIndex="0">
                    <ap:Collapsed Collapsed="false" Dirty="0000000000000001"/>
                </ap:TopicViewGroup>
                <ap:Text Dirty="0000000000000001" PlainText="11" ReadOnly="false">
                    <ap:Font/>
                </ap:Text>
            </ap:Topic>
            <ap:Topic Dirty="0000000000000001" OId="7qQAOqWlmkKn4t4Z2DMd5A==" Gen="0000000000000000">
                <ap:SubTopics>
                    <ap:Topic Dirty="0000000000000001" OId="ro5nEHjge0qcWHXXO7/8sA==" Gen="0000000000000000">
                        <ap:TopicViewGroup ViewIndex="0"/>
                        <ap:Text Dirty="0000000000000001" PlainText="1221" ReadOnly="false">
                            <ap:Font/>
                        </ap:Text>
                    </ap:Topic>
                    <ap:Topic Dirty="0000000000000001" OId="R5CK83KQTEquBBqivm8Lmg==" Gen="0000000000000000">
                        <ap:SubTopics>
                            <ap:Topic Dirty="0000000000000001" OId="xGn0UoleIU681h90xtm7oQ==" Gen="0000000000000000">
                                <ap:TopicViewGroup ViewIndex="0"/>
                                <ap:Text Dirty="0000000000000001" PlainText="12223" ReadOnly="false">
                                    <ap:Font/>
                                </ap:Text>
                            </ap:Topic>
                        </ap:SubTopics>
                        <ap:TopicViewGroup ViewIndex="0">
                            <ap:Collapsed Collapsed="false" Dirty="0000000000000001"/>
                        </ap:TopicViewGroup>
                        <ap:Text Dirty="0000000000000001" PlainText="1222" ReadOnly="false">
                            <ap:Font/>
                        </ap:Text>
                    </ap:Topic>
                </ap:SubTopics>
                <ap:TopicViewGroup ViewIndex="0">
                    <ap:Collapsed Collapsed="false" Dirty="0000000000000001"/>
                </ap:TopicViewGroup>
                <ap:Text Dirty="0000000000000001" PlainText="12" ReadOnly="false">
                    <ap:Font/>
                </ap:Text>
            </ap:Topic>
            <ap:Topic Dirty="0000000000000001" OId="jH0FY5i1/Uu7cPCFcFgoCQ==" Gen="0000000000000000">
                <ap:SubTopics>
                    <ap:Topic Dirty="0000000000000001" OId="TX7JXfIvn0WOpoqvUCKzUA==" Gen="0000000000000000">
                        <ap:TopicViewGroup ViewIndex="0"/>
                        <ap:Text Dirty="0000000000000001" PlainText="21" ReadOnly="false">
                            <ap:Font/>
                        </ap:Text>
                    </ap:Topic>
                    <ap:Topic Dirty="0000000000000001" OId="mOwoPsJM20SjK2j7RwqNow==" Gen="0000000000000000">
                        <ap:SubTopics>
                            <ap:Topic Dirty="0000000000000001" OId="/BWUGWt6mE6vUXJZDRxskw==" Gen="0000000000000000">
                                <ap:TopicViewGroup ViewIndex="0"/>
                                <ap:Text Dirty="0000000000000001" PlainText="31" ReadOnly="false">
                                    <ap:Font/>
                                </ap:Text>
                            </ap:Topic>
                            <ap:Topic Dirty="0000000000000001" OId="sqYG4DtARUq2NplBb7atEg==" Gen="0000000000000000">
                                <ap:TopicViewGroup ViewIndex="0"/>
                                <ap:Text Dirty="0000000000000001" PlainText="32" ReadOnly="false">
                                    <ap:Font/>
                                </ap:Text>
                            </ap:Topic>
                            <ap:Topic Dirty="0000000000000001" OId="QfxZRuomGk6kllnrvrT25w==" Gen="0000000000000000">
                                <ap:TopicViewGroup ViewIndex="0"/>
                                <ap:Text Dirty="0000000000000001" PlainText="33" ReadOnly="false">
                                    <ap:Font/>
                                </ap:Text>
                            </ap:Topic>
                        </ap:SubTopics>
                        <ap:TopicViewGroup ViewIndex="0">
                            <ap:Collapsed Collapsed="false" Dirty="0000000000000001"/>
                        </ap:TopicViewGroup>
                        <ap:Text Dirty="0000000000000001" PlainText="22" ReadOnly="false">
                            <ap:Font/>
                        </ap:Text>
                    </ap:Topic>
                </ap:SubTopics>
                <ap:OneBoundary>
                    <ap:Boundary Dirty="0000000000000001" OId="paP2GTbrYEuUuE6rusovoA==" Gen="0000000000000000">
                        <ap:BoundaryViewGroup ViewIndex="0"/>
                        <ap:BoundaryShape BoundaryShape="urn:mindjet:SummaryArc" Dirty="0000000000000001"/>
                        <ap:OneSummaryTopic>
                            <ap:Topic Dirty="0000000000000001" OId="QjLln9LrlUSlfS87gzyGxA==" Gen="0000000000000000">
                                <ap:TopicViewGroup ViewIndex="0"/>
                                <ap:Text Dirty="0000000000000001" PlainText="Call out topic" ReadOnly="false">
                                    <ap:Font/>
                                </ap:Text>
                            </ap:Topic>
                        </ap:OneSummaryTopic>
                    </ap:Boundary>
                </ap:OneBoundary>
                <ap:TopicViewGroup ViewIndex="0">
                    <ap:Collapsed Collapsed="false" Dirty="0000000000000001"/>
                </ap:TopicViewGroup>
                <ap:Text Dirty="0000000000000001" PlainText="13" ReadOnly="false">
                    <ap:Font Color="ffff0000"/>
                </ap:Text>
            </ap:Topic>
        </ap:SubTopics>
        <ap:TopicViewGroup ViewIndex="0">
            <ap:Collapsed Collapsed="false" Dirty="0000000000000001"/>
        </ap:TopicViewGroup>
        <ap:SubTopicsShape SubTopicsVerticalAlignment="urn:mindjet:Middle" Dirty="0000000000000000"/>
    </ap:Topic>
</ap:OneTopic>

使用上面引用的代码,我得到了这个(有些&#34;如果&#34;条件已从引用的代码中删除)

--------1123
------1121
------1122
----11
------1221
--------12223
------1222
----12
------21
--------31
--------32
--------33
------22
--------Call out topic
----13
-----ffff0000

0 个答案:

没有答案