Script for XML File Edit Only Executes In Designer

Hello,

I have a script that executes off of a button click that works flawlessly from the designer in Test mode. However, once I launch the project and try to run it, it fails. The script opens an xml file, edits a few of the entries, and then saves it. I currently have it trying to read and update the file on my desktop to eliminate any network permission issues. The project is being ran off of my profile, which has full permissions. I removed all of the code that actually does any of the editing and saving, and it still gets the error, which leads me to believe its some sort of issue importing the xml library. Does anyone have any suggestions?

I removed a few lines from the code so it would display better on here, but the error is always on the line with “ET.parse” .

Code:

import xml.etree.ElementTree as ET
	try:
		#get selected filler
		fillerValue = event.source.parent.getComponent('FillerList').selectedValue
		MOValue = event.source.parent.getComponent('txtMO').text
		ProdIDValue = str(event.source.parent.getComponent('txtProdID').intValue)
		if  fillerValue == 1 or fillerValue == 2:
			if fillerValue == 1:
				fillerText = 'FILLER_1'
				fileSavePath = r"C:\Users\BYutz\Desktop\prod_perf_3.xml"
			else:
				fillerText = 'FILLER_2'
				fileSavePath = r"C:\Users\BYutz\Desktop\prod_perf_47.xml"
			
			#location of the sample xml to edit, it will save as a completely new file
			tree = ET.parse(r"C:\Users\BYutz\Desktop\doNotDelete.xml")

Error:

Traceback (most recent call last):
  File "<event:actionPerformed>", line 19, in <module>
  File "<event:actionPerformed>", line 19, in <module>
  File "C:\Users\BYutz\.ignition\cache\gwcomo-svr-eng_8088\C1\pylib\xml\etree\ElementTree.py", line 1184, in parse
    tree.parse(source, parser)
  File "C:\Users\BYutz\.ignition\cache\gwcomo-svr-eng_8088\C1\pylib\xml\etree\ElementTree.py", line 651, in parse
    parser = XMLParser(target=TreeBuilder())
  File "C:\Users\BYutz\.ignition\cache\gwcomo-svr-eng_8088\C1\pylib\xml\etree\ElementTree.py", line 1480, in __init__
    parser = expat.ParserCreate(encoding, "}")
  File "C:\Users\BYutz\.ignition\cache\gwcomo-svr-eng_8088\C1\pylib\xml\parsers\expat.py", line 59, in ParserCreate
    return XMLParser(encoding, namespace_separator)
  File "C:\Users\BYutz\.ignition\cache\gwcomo-svr-eng_8088\C1\pylib\xml\parsers\expat.py", line 91, in __init__
    self._reader = XMLReaderFactory.createXMLReader(_xerces_parser_name)
java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser

org.xml.sax.SAXException: org.xml.sax.SAXException: SAX2 driver class org.apache.xerces.parsers.SAXParser not found
java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser

	caused by org.xml.sax.SAXException: SAX2 driver class org.apache.xerces.parsers.SAXParser not found
java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser

	caused by ClassNotFoundException: org.apache.xerces.parsers.SAXParser

Ignition v8.0.7 (b2019122014)
Java: Azul Systems, Inc. 11.0.5

Ignition’s client environment simply isn’t including that Apache jar. Consider using Java’s own XML processing classes–that’s almost certainly present.

Can you post the rest of the code?
We use the same classes and it works fine in the client and designer.

from __future__ import with_statement
import xml.etree.ElementTree as ET
import os, sys, codecs
import system
import re
getXML = system.file.openFile('.XML',"")
path = getXML
tree = ET.parse(path)
root = tree.getroot() 
context = root.getiterator(tag='DataType')

Here it is.

import xml.etree.ElementTree as ET
if system.gui.confirm(u'Are you sure you want to update CoLOS?', 'Confirm'):
	try:
		#get selected filler
		fillerValue = event.source.parent.getComponent('FillerList').selectedValue
		MOValue = event.source.parent.getComponent('txtMO').text
		ProdIDValue = str(event.source.parent.getComponent('txtProdID').intValue)
		if  fillerValue == 1 or fillerValue == 2:
			if fillerValue == 1:
				fillerText = 'FILLER_1'
				#fileSavePath = r"\\como-svr-colos\data\Import\o crap copies\test_prod_perf_3.xml"#r signifies a filepath so it will process properly
				fileSavePath = r"C:\Users\BYutz\Desktop\prod_perf_3.xml"
			else:
				fillerText = 'FILLER_2'
				fileSavePath = r"C:\Users\BYutz\Desktop\prod_perf_47.xml"
			
			#location of the sample xml to edit, it will save as a completely new file
			#tree = ET.parse(r"\\como-svr-eng\DocSample\doNotDelete.xml")
			tree = ET.parse(r"C:\Users\BYutz\Desktop\doNotDelete.xml")
			a = tree.find('ProductionResponse')
			b = a.find('SegmentResponse')
			
			#alter productID
			prodID = b.find('ID')
			prodID.text = ProdIDValue
			
			#alter MO
			moNumber = b.find('MONumber')
			moNumber.text = MOValue
			
			#get and alter filler number
			c = b.find('EquipmentActual')
			filler = c.find('EquipmentID')
			filler.text = fillerText
			
			#save the file
			tree.write(fileSavePath)
			system.gui.warningBox("Success!")
		else:
			system.gui.warningBox("Please select a filler.")
	except Exception, value:
		system.gui.errorBox(str(value.getCause()))

Can you try to set the path using the file open code?
Or double up on all of your backslashes in the path.

tree = ET.parse(r"C:\\Users\\BYutz\\Desktop\\doNotDelete.xml")

I tried both, and I get the same error. Thanks for the suggestion though! I feel like this has to be a bug. I am on 8.0.7 and will be upgrading as soon as we get some downtime.

Is there are reason its not included? Or is it a bug? It seems odd a feature would be set up to work in the designer but not in the client environment.

Not terribly odd. There’s lots of designer-UI stuff that doesn’t get loaded in the client, either. I ran into some of that with the Batik SVG libraries when building my SVG component.

This highlights the value of my general approach in jython: If there’s a java standard library that provides functionality that you need, use it instead of the python standard library. Even if both are available.

I didn’t have the choice of switching to Java classes because I am using a third party library that is in turn using the SAXParser. But, I got around it by moving the code to a Gateway script. Of course, I am parsing a really big Excel file, so it makes sense to do it on the Gateway anyway. May be overkill for a simple XML parse.

Hello,

Just wondering - were you ever able to come up with a solution/work-around for this issue that still utilized the same library?

I am currently dealing with almost the exact same identical problem as you had listed and happen to be getting the same import error - java.lang.ClassNotFoundException: org.apache.xerces.parsers.SAXParser

Works flawlessly in the designer, but fails in the project once it is opened.

Thanks

Sorry, I was not able to. I started to try using a java library, but was unable to get enough time to complete, not to mention frustrated that all my previous work was for nothing :slight_smile: . I just run the utility in the designer when I need it.

1 Like