New conversion from python parsing to Java

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 following changes, I added functions from this example, and I am unsure about the implementation and how can I look at the input and output. https://forum.inductiveautomation.com/t/xml-parsing-failing-every-10-runs-of-code/21838

	#tree = ET.parse(filePath)
	#logger.info('After: tree = ET.parse(filePath)')
	#xmlFile = tree.getroot()
	xmlFile = shared.xml.strToDoc(filePath)

What do you mean by lock up? Can you supply the full error your encountering?

First, strToDoc() doesn’t take a file path. It takes a string with all of the XML. To read and parse a file, use the inputToDoc().

You get back a Document, the root node of a Document Object Model.

A Document is able to create new nodes for your use elsewhere in the tree, and the root can be traversed with the Node interface’s methods. I usually use .firstChild and .nextSibling when iterating through everything. But .childNodes is also convenient.

From the Document itself you can use the .getElementsBy*() methods. Convenient when looking for specific elements.

XML Parsing Error

	for device in importDevices:
		#read the xml file
		filePath = '%s/%s.xml' % (rootPath,device)
		tree = inputToDoc(filePath)
		dom = docToStr(tree)
		xmlFile = strToDoc(dom)

I think if I am following correctly, I need to verify to formats I am passing in to the strToDoc function.
Which makes sense because the last line throws this error:

Caused by: org.python.core.PyException: Traceback (most recent call last): File “”, line 6, in File “”, line 334, in deviceXmlAutoImport File “”, line 283, in strToDoc File “”, line 277, in inputToDoc TypeError: parse(): 1st arg can’t be coerced to java.io.File, java.io.InputStream, org.xml.sax.InputSource, String

What exactly are you trying to do with this file? Because your code makes no sense unless you are simply trying to test reversibility. tree and xmlFile would end up identical–both of them Document objects.

I would like to replace the parsing section of my larger code with the new method so I can avoid the lock ups I am having with Elementtree.

This code, which I think I am supposed to figure out how to navigate the xml, to make the tag data structures the same so they can be worked on later in the code.

rootPath = 'C:\Canberra\Horizon\Ignition\DA'
device = 'dataanalyst9834'
#read the xml file
filePath = '%s/%s.xml' % (rootPath,device)
print 'reading %s' % filePath

print 'ET Test'
testTree= ET.parse(filePath)
print testTree
xml = testTree.getroot()
print 'OUTPUT1 xml: ', xml


print 'Java DOM Test'
#XmlDoc = inputToDoc('C:\Bob\XmlParseTest\dataagg0033.xml')
XmlDoc = inputToDoc(filePath)
XmlVersion = XmlDoc.getXmlVersion()
inputEncoding = XmlDoc.getInputEncoding()
XmlEncoding = XmlDoc.getXmlEncoding()
XmlStandalone = XmlDoc.getXmlStandalone()
print 'XmlVersion: ' + XmlVersion
print 'inputEncoding: ' + inputEncoding
print 'XmlEncoding: ' + XmlEncoding
print 'XmlStandalone: ' + str(XmlStandalone)

nodeList = XmlDoc.getElementsByTagName('Tags')
print 'nodeList: ', nodeList
#for tagName in tagNameList:
#             print tagName
print 'Java: ', XmlDoc.firstChild.tagName

nodeList = XmlDoc.getElementsByTagName('Tag')
print 'nodeList: ', nodeList
#for tagName in tagNameList:
#             print tagName
print 'Java firstChild.tagName: ', XmlDoc.firstChild.tagName
nodeList = XmlDoc.getChildNodes()
print nodeList
next = nodeList.getNextSibling()
print next
print 'Java lastChild.tagName: ', XmlDoc.lastChild.tagName```

Here is the output where I would look for aa way to find using the Java functions the format and value of OUTPUT1.

Do I need to open the file source folder and pass that as source argument to inpuToDoc?
or should I be using strToDoc?
Does inputToDoc take a filepath if it includes the fully named path including the file?
if not how it works, should i read in the contents outside of this
should i use other methods to read in the file?

inputToDoc() is just delegating to a DocumentBuilder’s .parse() methods. Jython will pick the one that matches the object type you give it. I highly recommend you browse the JavaDoc while looking at my xml.py file.

Hint: You probably want to load your filePath into a java File object and pass that to inputToDoc.

Meanwhile, can you share a sample of your XML?

dataanalyst9834.xml (825.5 KB)

I don’t know enough about element tree to know if this is possible. I don’t ever use it. Maybe with custom python wrapper classes for the java classes you could emulate ETree.

I’d just write to the java API and be done with it.