Python a XML vracia none

Python a XML vracia none
« kdy: 15. 02. 2021, 17:00:57 »
Zdravim.
Snazi sa s XML dokumentu vycitat par element ale vracia mi to vzdy none.
Toto mam v pythone
Kód: [Vybrat]
import xml.etree.ElementTree as ET
from urllib.request import urlopen
with urlopen('http://XXX/WebSvc/FCMSWebSvc.asmx/GetInkSmartTagParameters?KinkName=oreo&printerRange=&config_revision=') as data:
    tree = ET.parse(data)
    root = tree.getroot()

   
    ConfigHeaderId = tree.find('ConfigHeaderId')
    Config_Id = tree.find('Config_Id')
    Printer_Range = tree.find('Printer_Range')
    Config_Type = tree.find('Config_Type')
    Config_Revision = tree.find('Config_Revision')
    Status = tree.find('Status')
    print(Status,Config_Revision ,Config_Type)
XML vyzera takto
Kód: [Vybrat]
<TagParameters>
<ConfigHeaderInfo>
<ConfigHeaderItem>
<ConfigHeaderId>78578545</ConfigHeaderId>
<Config_Id>505</Config_Id>
<King>SMARTTAG</King>
<Config_Type>STANDARD</Config_Type>
<Config_Revision>15</Config_Revision>
<Status>Released</Status>
</ConfigHeaderItem>
</ConfigHeaderInfo>
v com je problem?
« Poslední změna: 15. 02. 2021, 19:03:59 od Petr Krčmář »


tecka

  • ***
  • 138
    • Zobrazit profil
    • E-mail
Re:Python a XML vracia none.
« Odpověď #1 kdy: 15. 02. 2021, 17:49:05 »
v com je problem?
V tom, že to XML má nějakou strukturu. Jestli chceš prostě ten tag vyhledat kdekoliv, tak to můžeš napsat jako
Kód: [Vybrat]
find('.//ConfigHeaderId')

Re:Python a XML vracia none.
« Odpověď #2 kdy: 15. 02. 2021, 18:31:56 »
nech skusam hocico nechce mi to vratit hodnotu s elementu len
Kód: [Vybrat]
<Element 'ConfigHeaderId' at 0x000001C1E1F912C0>

Re:Python a XML vracia none
« Odpověď #3 kdy: 15. 02. 2021, 19:12:13 »
python ma tu super vlastnost, ze si s nim muzes povidat. ja to delam tak ze kdyz mi neco nejde, tak si otevru python REPL, nahodim si tam minimalni priklad a pouzivam autocomplete a dir() funkci.
dalsi vec co je na pythonu super ze ma fakt dobrou dokumentaci - podival ses tam vubec?
https://docs.python.org/3/library/xml.etree.elementtree.html
Kód: [Vybrat]
We can import this data by reading from a file:

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
Or directly from a string:

root = ET.fromstring(country_data_as_string)
fromstring() parses XML from a string directly into an Element, which is the root element of the parsed tree. Other parsing functions may create an ElementTree. Check the documentation to be sure.

As an Element, root has a tag and a dictionary of attributes:

>>>
>>> root.tag
'data'
>>> root.attrib
{}
It also has children nodes over which we can iterate:

>>>
>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
Children are nested, and we can access specific child nodes by index:

>>>
>>> root[0][1].text
'2008'