There are a couple of different ways to import the XML data from etree, depending on how it is being stored. It can pull the data in from an XML file using the filepath, or it can read a string. Notice how regardless of how we import the XML, we end up with a root object.
# The library must first be imported no matter how we pull in the data. import xml.etree.ElementTree as ET # Here we can grab the filepath using Ignition's built in openFile function, parse that into a tree, then grab the root element. filepath = system.file.openFile() tree = ET.parse(filepath) root = tree.getroot() |
# The library must first be imported no matter how we pull in the data. import xml.etree.ElementTree as ET # Alternately, we can start with a string of the xml data. xmlString = """ <employee id="1234"> <name>John Smith</name> <start_date>2010-11-26</start_date> <department>IT</department> <title>Tech Support</title> </employee> """ # Then parse through the string using a different function that takes us straight to the root element. root = ET.fromstring(xmlString) |
Each Tag is considered an element object. In the example above, the root element would be the employee Tag. Elements can also have attributes, which are within the Tag itself. In the example above, the employee element has an attribute id with a value 1234. Finally, each element can also have additional data, typically in the form of a string. This additional data is usually placed in between the element's start and end Tags. In the example above, the employee element has no additional data, but its children do. The name element would have an additional data value of John Smith. All of this data can be accessed using the Element object's built-in functionality. The major functions are listed below, and each example uses the reading from a string root XML example from above.
Function | Description | Example | Output | |
---|---|---|---|---|
Element.tag | Returns the name of the Element's Tag. |
| employee | |
Element.attrib | Returns a dictionary of the Element's attributes. |
| {'id':'1234'} | |
Element.text | Returns the additional data of the Element. The example here will return nothing because the root does not have any text. The next example uses children which do have text. |
| ||
for child in Element | Will iterate through the Element's children. Each child is then its own element, complete with Tag, attrib, and text properties. |
| name John Smith | |
Element[index] | Allows direct reference to an Element's children by index. Since Tags can be nested many times, further nested children can be accessed by adding an additional index in square brackets as many times as necessary: Element[1][4][0] From the original element, we would go to the child located in the first position, that child's fourth position child, and that child's zero position child. When direct referencing child elements in this way, they still have access to the Tag, attrib, and text properties. |
| department Tech Support |
Using the functions above, we can now easily parse through an XML file and use the results for something. Lets keep it simple, and parse through a document and then place the values into a table. First we need to start with an XML document. We have one below for you to test with in a string form, which would need to be pasted at the top of the script.
|
We can then place a Table component and a Button component on the window, and place this script on the Button's actionPerformed event.
# Start by importing the library import xml.etree.ElementTree as ET ###### # Here is where you would paste in the document string. # Simply remove this comment, and paste in the document string. ###### # We can then parse the string into useable elements. root = ET.fromstring(document) # This creates empty header and row lists that we will add to later. # These are used to create the dataset that will go into the Table. # We could fill in the names of the headers beforehand, since we know what each will be. # However, this allows us to add or remove children keys, and the script will automatically adjust. headers = [] rows = [] # Now we can loop through each child of the root. # Since the root is catalog, each child element is an individual book. # We also create a single row empty list. We can add all of the data for a single book to this list. for child in root: oneRow = [] # Check if the book has any attributes. if child.attrib != {}: # If it does contain attributes, we want to loop through all of them. for key in child.attrib: # Since we only want to add the attributes to our header list once, first check if it is there. # If it isn't add it. if key not in headers: headers.append(key) # Add the attribute value to the oneRow list. oneRow.append(child.attrib[key]) # Loop through the children of the book. for child2 in child: # Similar to above, we check if the tag is present in the header list before adding it. if child2.tag not in headers: headers.append(child2.tag) # We can then add the text of the Element to the oneRow list. oneRow.append(child2.text) # Finally, we want to add the oneRow list to our list of rows. rows.append(oneRow) # Once the loop is complete, this will print out the headers and rows list so we can manually check them in the console. print headers print rows # Convert to a dataset, and insert into the Table. data = system.dataset.toDataSet(headers, rows) event.source.parent.getComponent('Table').data = data |