# Parsing XML with Etree

**URL:** <https://forum.inductiveautomation.com/t/parsing-xml-with-etree/50723>\
**Category:** Ignition\
**Created:** [September 8, 2021, 4:59pm UTC](https://forum.inductiveautomation.com/t/parsing-xml-with-etree/50723 "2021-09-08T16:59:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dion.botha](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dion.botha](https://forum.inductiveautomation.com/u/dion.botha)\
**Post date:** [September 8, 2021, 4:59pm UTC](https://forum.inductiveautomation.com/t/parsing-xml-with-etree/50723/1 "2021-09-08T16:59:25Z")

</div>

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:

```auto
<?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](https://docs.inductiveautomation.com/display/DOC80/Parsing+XML+with+the+Etree+Library)

So I do this:

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

```

Unfortunately it throws an error:

```auto
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?

---

<div class="post-metadata">

**Author:** ![dion.botha](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dion.botha](https://forum.inductiveautomation.com/u/dion.botha)\
**Post date:** [September 8, 2021, 5:24pm UTC](https://forum.inductiveautomation.com/t/parsing-xml-with-etree/50723/2 "2021-09-08T17:24:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 8, 2021, 5:54pm UTC](https://forum.inductiveautomation.com/t/parsing-xml-with-etree/50723/3 "2021-09-08T17:54:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dion.botha](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dion.botha](https://forum.inductiveautomation.com/u/dion.botha)\
**Post date:** [September 8, 2021, 7:15pm UTC](https://forum.inductiveautomation.com/t/parsing-xml-with-etree/50723/4 "2021-09-08T19:15:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 8, 2021, 8:27pm UTC](https://forum.inductiveautomation.com/t/parsing-xml-with-etree/50723/5 "2021-09-08T20:27:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dion.botha](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@dion.botha](https://forum.inductiveautomation.com/u/dion.botha)\
**Post date:** [September 8, 2021, 10:02pm UTC](https://forum.inductiveautomation.com/t/parsing-xml-with-etree/50723/6 "2021-09-08T22:02:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dmitry\_Bubnenkov](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dmitry_bubnenkov/32/59614_2.png) [@Dmitry\_Bubnenkov](https://forum.inductiveautomation.com/u/Dmitry_Bubnenkov)\
**Post date:** [March 18, 2024, 11:02am UTC](https://forum.inductiveautomation.com/t/parsing-xml-with-etree/50723/7 "2024-03-18T11:02:00Z")

</div>

You could try another approach for parsing XML. See example: [How to Load an XML Set into the Database](https://redata.dev/smartxml/docs/how-to-load-multiple-xml-into-database.html)
