0

I am no able to make python to read xlm info. Please see my xml Line bellow. Do not seems to be normal as other I have seen before.

b'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<mtsInputForm source="None">\n    <type>2109</type>\n    <serial>000777R</serial>\n    <xmlMessages>\n        <xmlMessage type="system"/>\n        <xmlMessage type="error" num="1">No information was found matching the search criteria.</xmlMessage>\n    </xmlMessages>\n</mtsInputForm>\n'

I need to get this message "No information was found matching the search criteria" from the XML line above. How do I do that?

Alex DS
  • 81
  • 8
  • 1
    Possible duplicate of [How do I parse XML in Python?](https://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python) – manveti Mar 29 '19 at 18:48

2 Answers2

0

Check this example from Python Cookbook on how to Parse Simple XML Data (Section 6.3):

from urllib.request import urlopen
from xml.etree.ElementTree import parse

# Download the RSS feed and parse it
u = urlopen('http://planet.python.org/rss20.xml')
doc = parse(u)

# Extract and output tags of interest
for item in doc.iterfind('channel/item'):
  title = item.findtext('title')
  date = item.findtext('pubDate')
  link = item.findtext('link')

  print(title)
  print(date)
  print(link)
cavalcantelucas
  • 1,362
  • 3
  • 12
  • 34
  • All the example i see seems to very simple xml items name like the example that you showd me title - pubDate - ,link.... but the one I have , has the following name that I have to extract data from data i want – Alex DS Mar 30 '19 at 00:42
0

I just found out....

XML_line = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n<mtsInputForm source="None">\n    <type>2109</type>\n    <serial>000777R</serial>\n    <xmlMessages>\n        <xmlMessage type="system"/>\n        <xmlMessage type="error" num="1">No information was found matching the search criteria.</xmlMessage>\n    </xmlMessages>\n</mtsInputForm>\n'''
source = str.encode(XML_line)
root = ET.fromstring(source)
for msg in root.findall(".//xmlMessage"): #xlm has two tgs with same name "xmlMessage" 
    information = msg.text
            if info_typee == str:
                info_list.append(info)
                print (information)

xlm has two tgs with same name "xmlMessage" and the code above bring both information, one is type = class the other is str

Alex DS
  • 81
  • 8