# How to parse a xml file with a script?

**URL:** <https://forum.inductiveautomation.com/t/how-to-parse-a-xml-file-with-a-script/3514>\
**Category:** Ignition\
**Created:** [October 28, 2011, 9:19am UTC](https://forum.inductiveautomation.com/t/how-to-parse-a-xml-file-with-a-script/3514 "2011-10-28T09:19:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mazeyrat](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mazeyrat/32/10955_2.png) [@mazeyrat](https://forum.inductiveautomation.com/u/mazeyrat)\
**Post date:** [October 28, 2011, 9:19am UTC](https://forum.inductiveautomation.com/t/how-to-parse-a-xml-file-with-a-script/3514/1 "2011-10-28T09:19:12Z")

</div>

I try to parse a xml file in a script module.

what is the simple way :  
import java classes like javax ???  
or there is existing function in jython ??? the import xml.parsers.expat dont work ?

---

<div class="post-metadata">

**Author:** ![f\_jons](https://avatars.discourse-cdn.com/v4/letter/f/9fc29f/32.png) [@f\_jons](https://forum.inductiveautomation.com/u/f_jons)\
**Post date:** [October 28, 2011, 6:18pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-a-xml-file-with-a-script/3514/2 "2011-10-28T18:18:24Z")

</div>

I do this to dynamically build multi-state buttons. Basically, I store the XML data in the database and pass it to this script which I have in the script modules.  
Here is an example XML file.

`<data>
  <productType>
    <indicatorValue>0</indicatorValue>
    <displayText>Casement / Picture</displayText>
    <buttonValue>IFCA-CAP</buttonValue>
    <plcValue>3</plcValue>
  </productType>
  <productType>
    <indicatorValue>1</indicatorValue>
    <displayText>Awning / Transom</displayText>
    <buttonValue>IFAWN-TR</buttonValue>
    <plcValue>4</plcValue>
  </productType>
  <productType>
    <indicatorValue>2</indicatorValue>
    <displayText>Double / Single Hung</displayText>
    <buttonValue>IFSH-DH</buttonValue>
    <plcValue>1</plcValue>
  </productType>
  <productType>
    <indicatorValue>3</indicatorValue>
    <displayText>DH Pict/Trans</displayText>
    <buttonValue>IFSH-DH-P-TR</buttonValue>
    <plcValue>5</plcValue>
  </productType>
  <productType>
    <indicatorValue>4</indicatorValue>
    <displayText>Glider</displayText>
    <buttonValue>IFGL</buttonValue>
    <plcValue>2</plcValue>
  </productType>
  <productType>
    <indicatorValue>5</indicatorValue>
    <displayText>Triple Glider</displayText>
    <buttonValue>IFGLTS</buttonValue>
    <plcValue>8</plcValue>
  </productType>
</data>`  
To parse the above XML, I would pass it to this definition as a string for the first variable, and the second variable is the name of the root node, ie “productType”. So the call would look like this:

`app.Buttons.parseTypeButtonXML(xmlString, "productType")
`  
I had to do it this way to make it dynamic. You should be able to extract whatever you need from the definition to parse your XML.

[code]# This definition parses xml for building type buttons and stores the data in a list of lists

# in the format [[indicatorValue, displayText, buttonValue], [indicatorValue, displayText, buttonValue]…]

# @param xmlString This is a string containing xml button defintions.

# @param buttonType A string representing the root xml element, which defines

# what type of button we are building.

def parseTypeButtonXML(xmlString, buttonType):  
# Import the necessary definitions.  
from javax.xml.parsers import DocumentBuilder, DocumentBuilderFactory  
from org.xml.sax import InputSource  
from org.w3c.dom import Element, Node, NodeList  
from [java.io](http://java.io) import StringReader  
# Create a empty list for the button data.  
buttonData = []  
# Build the parser.  
factory = DocumentBuilderFactory.newInstance()  
db = factory.newDocumentBuilder()  
inStream = InputSource()  
inStream.setCharacterStream(StringReader(xmlString))  
doc = db.parse(inStream)  
# Get the nodeList as elements of ‘buttonType’  
nodeList = doc.getElementsByTagName(buttonType)  
# Work through each sashType.  
for sashType in range(nodeList.getLength()):  
# create a temporary list so we can add the indicator value and the display text.  
temp = []  
node = nodeList.item(sashType)  
if( node.getNodeType() == Node.ELEMENT\_NODE ):  
indicatorNode = node.getElementsByTagName(‘indicatorValue’)  
displayTextNode = node.getElementsByTagName(‘displayText’)  
buttonValueNode = node.getElementsByTagName(‘buttonValue’)  
plcValueNode = node.getElementsByTagName(‘plcValue’)  
for property in range(indicatorNode.getLength()):  
if( indicatorNode.item(property).getNodeType() == Node.ELEMENT\_NODE ):  
temp.append(indicatorNode.item(property).getFirstChild().getNodeValue())  
if( displayTextNode.item(property).getNodeType() == Node.ELEMENT\_NODE ):  
temp.append(displayTextNode.item(property).getFirstChild().getNodeValue())  
if( buttonValueNode.item(property).getNodeType() == Node.ELEMENT\_NODE ):  
temp.append(buttonValueNode.item(property).getFirstChild().getNodeValue())  
if( plcValueNode.item(property).getNodeType() == Node.ELEMENT\_NODE ):  
temp.append(plcValueNode.item(property).getFirstChild().getNodeValue() )  
# Add additional if statements for each new node…  
# Append each temp list to the buttonData list.  
buttonData.append(temp)  
return buttonData

#—END of parseTypeButtonXML() DEFINITION[/code]

---

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [October 28, 2011, 7:13pm UTC](https://forum.inductiveautomation.com/t/how-to-parse-a-xml-file-with-a-script/3514/3 "2011-10-28T19:13:47Z")

</div>

Pretty intense, f\_jons! Mind providing a higher level description of how you are using dynamically created multistate buttons?
