# New conversion from python parsing to Java

**URL:** <https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202>\
**Category:** 3rd Party Modules\
**Created:** [July 26, 2021, 5:31pm UTC](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202 "2021-07-26T17:31:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [July 26, 2021, 5:31pm UTC](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202/1 "2021-07-26T17:31:28Z")

</div>

Here is the current block of code where I am using the parse with jython solution that tends to lock up every so often.

```auto
	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](https://forum.inductiveautomation.com/t/xml-parsing-failing-every-10-runs-of-code/21838)

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

```

---

<div class="post-metadata">

**Author:** ![ryanjmclaughlin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryanjmclaughlin/32/4318_2.png) [@ryanjmclaughlin](https://forum.inductiveautomation.com/u/ryanjmclaughlin)\
**Post date:** [July 26, 2021, 6:20pm UTC](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202/2 "2021-07-26T18:20:00Z")

</div>

> [@AJBerera](#):
>
> tends to lock up

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

---

<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:** [July 26, 2021, 8:01pm UTC](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202/3 "2021-07-26T20:01:20Z")

</div>

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](https://docs.oracle.com/javase/8/docs/api/org/w3c/dom/Document.html), 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](https://docs.oracle.com/javase/8/docs/api/org/w3c/dom/Node.html) 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.

---

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [July 26, 2021, 8:05pm UTC](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202/4 "2021-07-26T20:05:35Z")

</div>

XML Parsing Error

---

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [July 27, 2021, 10:01pm UTC](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202/5 "2021-07-27T22:01:42Z")

</div>

```auto
	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

---

<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:** [July 28, 2021, 11:53am UTC](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202/6 "2021-07-28T11:53:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [July 29, 2021, 9:21pm UTC](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202/7 "2021-07-29T21:21:11Z")

</div>

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.

````auto
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?
````

---

<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:** [July 29, 2021, 9:45pm UTC](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202/8 "2021-07-29T21:45:06Z")

</div>

inputToDoc() is just delegating to a [DocumentBuilder](https://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilder.html)’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](https://docs.oracle.com/javase/8/docs/api/java/io/File.html) object and pass that to inputToDoc.

Meanwhile, can you share a sample of your XML?

---

<div class="post-metadata">

**Author:** ![AJBerera](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@AJBerera](https://forum.inductiveautomation.com/u/AJBerera)\
**Post date:** [July 29, 2021, 10:40pm UTC](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202/9 "2021-07-29T22:40:34Z")

</div>

[dataanalyst9834.xml](https://forum.inductiveautomation.com/uploads/short-url/nu3aZTb0bmoplFivBzwxaNHk8MD.xml) (825.5 KB)

---

<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:** [July 30, 2021, 12:48am UTC](https://forum.inductiveautomation.com/t/new-conversion-from-python-parsing-to-java/49202/10 "2021-07-30T00:48:01Z")

</div>

> [@AJBerera](#):
>
> to make the tag data structures the same so they can be worked on later in the code

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.
