簡體   English   中英

Python 3 xml 解析打印問題

[英]Python 3 xml parsing and printing problem

我有一個樣本 xml 文件如下

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!--Created with JFLAP 7.1.--><structure>&#13;
    <type>fa</type>&#13;
    <automaton>&#13;
        <!--The list of states.-->&#13;
        <state id="0" name="q1">&#13;
            <x>104.0</x>&#13;
            <y>149.0</y>&#13;
            <initial/>&#13;
        </state>&#13;
        <state id="1" name="q2">&#13;
            <x>210.0</x>&#13;
            <y>153.0</y>&#13;
            <final/>&#13;
        </state>&#13;
        <state id="2" name="q3">&#13;
            <x>292.0</x>&#13;
            <y>155.0</y>&#13;
        </state>&#13;
        <!--The list of transitions.-->&#13;
        <transition>&#13;
            <from>0</from>&#13;
            <to>0</to>&#13;
            <read>0</read>&#13;
        </transition>&#13;
        <transition>&#13;
            <from>1</from>&#13;
            <to>1</to>&#13;
            <read>1</read>&#13;
        </transition>&#13;
        <transition>&#13;
            <from>0</from>&#13;
            <to>1</to>&#13;
            <read>1</read>&#13;
        </transition>&#13;
        <transition>&#13;
            <from>2</from>&#13;
            <to>1</to>&#13;
            <read>0</read>&#13;
        </transition>&#13;
        <transition>&#13;
            <from>1</from>&#13;
            <to>2</to>&#13;
            <read>0</read>&#13;
        </transition>&#13;
        <transition>&#13;
            <from>2</from>&#13;
            <to>1</to>&#13;
            <read>1</read>&#13;
        </transition>&#13;
    </automaton>&#13;
</structure>

我正在嘗試打印state標簽的所有name屬性,除了id到目前為止,這是我的代碼:

import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
for automaton in root.findall('automaton'):
    state = automaton.find('state')
    state_name = state.get('name')
    print(state_name)

我當前的 output:

q1

我的預期 output:

q1
q2
q3

我很困惑,因為我代碼中的for循環不會打印所有name屬性嗎? 我哪里做錯了,我該如何解決? 謝謝~

您的代碼中缺少另一個findall 有多個state標簽。

請參閱下面的代碼。 (我用文本 io 緩沖區替換了一個文件。)

    xml = """<?xml version="1.0" encoding="UTF-8" standalone="no"?><!--Created with JFLAP 7.1.--><structure>&#13;
    <type>fa</type>&#13;
    <automaton>&#13;
        <!--The list of states.-->&#13;
        <state id="0" name="q1">&#13;
            <x>104.0</x>&#13;
            <y>149.0</y>&#13;
            <initial/>&#13;
        </state>&#13;
        <state id="1" name="q2">&#13;
            <x>210.0</x>&#13;
            <y>153.0</y>&#13;
            <final/>&#13;
        </state>&#13;
        <state id="2" name="q3">&#13;
            <x>292.0</x>&#13;
            <y>155.0</y>&#13;
        </state>&#13;
        <!--The list of transitions.-->&#13;
        <transition>&#13;
            <from>0</from>&#13;
            <to>0</to>&#13;
            <read>0</read>&#13;
        </transition>&#13;
        <transition>&#13;
            <from>1</from>&#13;
            <to>1</to>&#13;
            <read>1</read>&#13;
        </transition>&#13;
        <transition>&#13;
            <from>0</from>&#13;
            <to>1</to>&#13;
            <read>1</read>&#13;
        </transition>&#13;
        <transition>&#13;
            <from>2</from>&#13;
            <to>1</to>&#13;
            <read>0</read>&#13;
        </transition>&#13;
        <transition>&#13;
            <from>1</from>&#13;
            <to>2</to>&#13;
            <read>0</read>&#13;
        </transition>&#13;
        <transition>&#13;
            <from>2</from>&#13;
            <to>1</to>&#13;
            <read>1</read>&#13;
        </transition>&#13;
    </automaton>&#13;
</structure>"""


from io import StringIO
import xml.etree.ElementTree as ET

xml_file = StringIO(xml)
tree = ET.parse(xml_file)
root = tree.getroot()
for automaton in root.findall('automaton'):
    for state in automaton.findall('state'): # find all the 'state' tags
        state_name = state.get('name')
        print(state_name)

OUTPUT:

q1
q2
q3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM