Parsing XML in GH Python Script Editor?

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

to read and process 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

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…

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…

1 Like

Hi @robe5177

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?

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

liftbridges.xml (4.3 KB) Parse_GH_WIP_R0.gh (8.2 KB)

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.

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 (9.7 KB)

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

2 Likes

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