# Simple xml parsing question

**URL:** <https://forum.inductiveautomation.com/t/simple-xml-parsing-question/89232>\
**Category:** Ignition\
**Tags:** scripting, ignition81\
**Created:** [April 22, 2024, 7:17pm UTC](https://forum.inductiveautomation.com/t/simple-xml-parsing-question/89232 "2024-04-22T19:17:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [April 22, 2024, 7:17pm UTC](https://forum.inductiveautomation.com/t/simple-xml-parsing-question/89232/1 "2024-04-22T19:17:05Z")

</div>

I have basically no experience parsing xml in Ignition / Python. I'm trying to figure out the easiest way I can parse the following xml:

`<?xml version=1.0 encoding=utf-8?><serial>XA8287</serial>`

...such that I can get the serial into a string for further manipulation. The serial in question here would be `XA8287`. This is taking place within a Web Dev script, Ignition version 8.1.32.

Do I need to import DocumentBuilderFactory as explained in [this page](https://docs.inductiveautomation.com/docs/8.1/platform/scripting/scripting-examples/parsing-xml-with-the-etree-library)? Or would the SAX Parser be better? I feel like I could arrive at something workable by playing around a bit but I have no idea what the 'best practice' method would be here...

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 22, 2024, 7:24pm UTC](https://forum.inductiveautomation.com/t/simple-xml-parsing-question/89232/2 "2024-04-22T19:24:46Z")

</div>

For a small XML document like your example the DOM Parser / DocumentBuilderFactory example should be easier.

SAX (streaming) is harder to use but better memory-wise when dealing with large documents.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 22, 2024, 7:25pm UTC](https://forum.inductiveautomation.com/t/simple-xml-parsing-question/89232/3 "2024-04-22T19:25:53Z")

</div>

There's also the Python etree library you can probably find examples for here in the forum, but I think there's historically been weird / hard to track down errors when using that...

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 22, 2024, 7:27pm UTC](https://forum.inductiveautomation.com/t/simple-xml-parsing-question/89232/4 "2024-04-22T19:27:00Z")

</div>

Related:

> [@Download file as xml](https://forum.inductiveautomation.com/t/download-file-as-xml/84363/2):
>
> Good morning Gerardo, Yes, but you'll have to generate the XML piece by piece. I would use my helper script module for this kind of work: [xml.py](https://www.automation-pros.com/ignition/xml.py) (Place it in your project script library.) Make a somewhat generic row-to-xml function in a project library, perhaps like so: rowTemplate = """\<table\>\<row/\>\</table\>""" # Produce downloadable XML bytes from a dictionary of column values. def rowToXML(valuesDict): dom = xml.strToDoc(rowTemplate) rowNode = dom.getElementsByTagName("row")[0] for k, …

I highly recommend using my wrappers around java's native XML handlers.

Edit: This might be more helpful:

> [@New conversion from python parsing to Java](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202):
>
> Here is the current block of code where I am using the parse with jython solution that tends to lock up every so often. import xml.etree.ElementTree as ET #read the xml filef for device in importDevices: filePath = '%s/%s.xml' % (rootPath,device) #print 'reading %s' % filePath deviceStart = system.date.now() #logger.info('Before: tree = ET.parse(filePath)') tree = ET.parse(filePath) #logger.info('After: tree = ET.parse(filePath)') xmlFile = tree.getroot() I made the f…

---

<div class="post-metadata">

**Author:** ![Bushmatic](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bushmatic/32/87014_2.png) [@Bushmatic](https://forum.inductiveautomation.com/u/Bushmatic)\
**Post date:** [April 23, 2024, 2:32pm UTC](https://forum.inductiveautomation.com/t/simple-xml-parsing-question/89232/5 "2024-04-23T14:32:44Z")

</div>

The element tree library seems to be working ok for now:

```python
def doPost(request, session):
	import xml.etree.ElementTree as ET
	xml = str(request['postData'])
	root = ET.fromstring(xml)
	serial = root.text
	...

```

And then it gets used once more at the end, after I construct an xml string:

```python
	...
	root = ET.fromstring(xml_string)
	return root

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 23, 2024, 2:49pm UTC](https://forum.inductiveautomation.com/t/simple-xml-parsing-question/89232/6 "2024-04-23T14:49:06Z")

</div>

> [@Bushmatic](#):
>
> element tree library seems to be working ok

IIRC, it **can** break if you feed it some unicode.

Don't use jython/python stdlib packages for things that can be done with java standard libraries.
