Parsing XML with Etree

Hi all,

I have a machine that is controlled with xml messages.

After forming a message, I send send it to the machine like this:
response = system.net.httpPost(URL, "text/xml", message)

if I print the response, I get this:

<?xml version="1.0" encoding="utf-8"?>

<response>

  <fault>

    <code>1002</code>

  </fault>

</response>

I would like to parse this string as described here: Parsing XML with the Etree Library - Ignition User Manual 8.0 - Ignition Documentation

So I do this:

import xml.etree.ElementTree as ET
root = ET.fromstring(response)

Unfortunately it throws an error:

Traceback (most recent call last):
  File "<input>", line 25, in <module>
  File "C:\Users\bothad\.ignition\cache\gwmeisign1.meissner.com_8043\C0\pylib\xml\etree\ElementTree.py", line 1314, in XML
    return parser.close()
  File "C:\Users\bothad\.ignition\cache\gwmeisign1.meissner.com_8043\C0\pylib\xml\etree\ElementTree.py", line 1667, in close
    self._raiseerror(v)
  File "C:\Users\bothad\.ignition\cache\gwmeisign1.meissner.com_8043\C0\pylib\xml\etree\ElementTree.py", line 1519, in _raiseerror
    raise err
  File "<string>", line None
xml.etree.ElementTree.ParseError: Content is not allowed in trailing section.

Any ideas on how I can overcome this error?

Did this to remove the last thing in the string and it is now working:
response = response[:-1]

response = response.strip() might be a better option; that will remove any leading and trailing whitespace.

Yea, I tried that but was still getting that error…

Hmm, I wonder if that trailing character is some non-whitespace non-printable character; repr() might help to see what exactly it is.

Thanks, found that there was a ‘\x00’ at the end. switched from using response = response[:-1] to response = response.rstrip('\x00')

1 Like

You could try another approach for parsing XML. See example: How to Load an XML Set into the Database