# Parsing XML in GH Python Script Editor?

**URL:** <https://discourse.mcneel.com/t/parsing-xml-in-gh-python-script-editor/89775>\
**Category:** Rhino Developer\
**Created:** [October 2, 2019, 6:52pm UTC](https://discourse.mcneel.com/t/parsing-xml-in-gh-python-script-editor/89775 "2019-10-02T18:52:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robe5177](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/robe5177/32/760556_2.png) [@robe5177](https://discourse.mcneel.com/u/robe5177)\
**Post date:** [October 2, 2019, 6:52pm UTC](https://discourse.mcneel.com/t/parsing-xml-in-gh-python-script-editor/89775/1 "2019-10-02T18:52:45Z")

</div>

Tried to implement this in R5: [https://docs.python.org/2/library/xml.etree.elementtree.html](https://docs.python.org/2/library/xml.etree.elementtree.html)

to read and process [http://www.canals.ny.gov/xml/liftbridges.xml](http://www.canals.ny.gov/xml/liftbridges.xml)

Code would be:  
import xml.etree.ElementTree as ET  
tree = ET.parse(‘country\_data.xml’)  
root = tree.getroot()

error: 1. Solution exception:expected str, got DataTree[object]

then tried:

from xml.etree import ElementTree # part of python distribution  
from elementtree import SimpleXMLTreeBuilder # part of your codebase  
ElementTree.XMLTreeBuilder = SimpleXMLTreeBuilder.TreeBuilder

with this error:

1. Solution exception:No module named elementtree

gHowl has an xml parser to obtain the info, challenge is breaking it up into usable formats.

Referred to this example: [https://discourse.mcneel.com/t/access-xml-data/1346/10](https://discourse.mcneel.com/t/access-xml-data/1346/10)

Wrote ths with y being the xml file path:  
filename = y  
xmldoc = System.Xml.XmlDocument()  
xmldoc.Load(filename)  
items = xmldoc.SelectNodes(“liftbridges/mile/”)  
for item in items:  
print item.InnerText

Not sure on best way to access it…

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [October 2, 2019, 7:09pm UTC](https://discourse.mcneel.com/t/parsing-xml-in-gh-python-script-editor/89775/2 "2019-10-02T19:09:10Z")

</div>

Hello,

I had a similar problem with Ironpython in Rhino 5. As I remember I ended up doing my xml parsing with a separate CPython script before importing the results into the Elk plugin.

I would be interested in an Ironpython solution…

---

<div class="post-metadata">

**Author:** ![piac](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/piac/32/769366_2.png) [@piac](https://discourse.mcneel.com/u/piac)\
**Post date:** [October 3, 2019, 3:26pm UTC](https://discourse.mcneel.com/t/parsing-xml-in-gh-python-script-editor/89775/3 "2019-10-03T15:26:38Z")

</div>

Hi @robe5177

> [@robe5177](#):
>
> error: 1. Solution exception:expected str, got DataTree[object]

This means that a `DataTree[object]` (a type that is very specific to the Grasshopper SDK) was fed to a library that only accepts `str` (strings) in that place.

Maybe there is a way to make this work: could you attach a small definition with something to test?

---

<div class="post-metadata">

**Author:** ![robe5177](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/robe5177/32/760556_2.png) [@robe5177](https://discourse.mcneel.com/u/robe5177)\
**Post date:** [October 4, 2019, 12:20am UTC](https://discourse.mcneel.com/t/parsing-xml-in-gh-python-script-editor/89775/5 "2019-10-04T00:20:35Z")

</div>

Ya: I can parse with the ET, but not with Steve’s Code. Probably some way to do it. Files to test:

 ![Parsing_GH_R0](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/a/6/a6e6e3ccec1bdf617b9879d7aec826c58e04d7ca.jpeg) [liftbridges.xml](https://discourse.mcneel.com/uploads/short-url/gR0br8UaBcm1RXNeL4SAUTIQjr1.xml) (4.3 KB) [Parse\_GH\_WIP\_R0.gh](https://discourse.mcneel.com/uploads/short-url/jhULcMXfyAknYxifkfzr9loEULo.gh) (8.2 KB)

---

<div class="post-metadata">

**Author:** ![piac](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/piac/32/769366_2.png) [@piac](https://discourse.mcneel.com/u/piac)\
**Post date:** [October 4, 2019, 1:32pm UTC](https://discourse.mcneel.com/t/parsing-xml-in-gh-python-script-editor/89775/6 "2019-10-04T13:32:36Z")

</div>

This works fine with both libraries in Rhino 6. Each can be used, and one is more “.Net”-y, and the other is more “classic Python”. Other than this, I don’t see any problem with any of them, and they give the same results.

 ![image](https://global.discourse-cdn.com/mcneel/uploads/default/original/3X/a/2/a2d2be10363e4f2cdf09233f49e8a2fb92b410e6.png)

In Rhino 5, only the second one will work, because the old version of IronPython did not include the `expat` module.

I’ve attached a fuller example for both of these, applied to your XML.

[for\_robe5177.gh](https://discourse.mcneel.com/uploads/short-url/j9x80TsddB2V5EXEQF19ejncKoP.gh) (9.7 KB)

Thanks,

Giulio

–  
Giulio Piacentino  
for Robert McNeel & Associates  
[giulio@mcneel.com](mailto:giulio@mcneel.com)

---

<div class="post-metadata">

**Author:** ![robe5177](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/robe5177/32/760556_2.png) [@robe5177](https://discourse.mcneel.com/u/robe5177)\
**Post date:** [October 5, 2019, 5:49am UTC](https://discourse.mcneel.com/t/parsing-xml-in-gh-python-script-editor/89775/7 "2019-10-05T05:49:06Z")

</div>

That’s great! thanks for writing up! Will test in v6 next week:)
