Access XML data

Hi @stevebaer,
How I can handle xml data into an external file?
I’m trying the import xml.etree.elementTree but it gives me an expat error.
I looked inside the directory and the parser folder it’s empty.

Do you have suggestion? I’ve tried the minidom but look much more complex, to me at least.

Thanks

PS: could you give me a short and basic example, I’m very lazy guy :wink:

Are you trying to read an xml file -or- create/write to an xml file?

I’m trying to read data from an xml, change some of them and then rewrite a new xml file.
(in fact I would like to write vrayforrhino material that are plain xml with custom extension)

this is a sample: http://www.vraywiki.it//Scenes/DefaultMaterial.vismat

Hi Riccardo, depending on the language used, you might try XMLDom or xml.Dom

c.

Thanks clement,

  1. it’s RhinoIronPython

  2. I already seen the xml.Dom link but I can’t find example or tutorial and Is not clear enough for me how to use it.

I, partially, solved installing the full Python release and overwriting manually all the xml folder in RhinoPython
(C:\Program Files\Rhinoceros 5 (64-bit)\Plug-ins\IronPython\Lib)

Not sure if this is the right way.

It works similar to the document object model in html and java. Once i accepted that every tag is an element or node with parent and child relationship holding just a value (the content of the tag) it was easier to understand for me. The tags can be nested. of course. Imagine this construction sheme for elements in the xml or html document:

<tagname>ContentText</tagname>

Nesting of elements can also be performed, it is just done by the concept of starting or ending tag as in html. In this case the element “Something” has a child element “BlaBla”:

<Something>
    <BlaBla>AnyString</BlaBla>
</Something>

Depending on the amount of shader options and parameters you need to write yourself as xml, i would start with a simple vismat and rebuild that from scratch. If you only have to re-create a few parameters you still can use the file system approach and write things to xml as strings. There should be no difference in the resulting xml file if you wite the header and all starting and ending tags properly.

For reading in the xml with large amount of dom nodes, it’s probably faster to use things like xml.Dom as you don’t have to detect all the the elements yourself.

EDIT: Here is the link to the one tutorial i´ve been working with in earlier days. It was for vbScript and i did not install anything.

c.

No! Don’t overwrite the lib folder. That is absolutely not the correct approach. We will be updating python to a newer version in (hopefully) SR6 that will have better support for the xml libraries in python, but overwriting what is already there is not the correct approach.

You can use .NET’s XmlDocument to do this task. I don’t know what VRay’s xml format is, but here is a sample that prints the contents of an attached xml file
xmltest.xml(154 Bytes)

import clr
clr.AddReference("System.Xml")
import System.Xml

filename = r"C:\Users\a-steve\Downloads\xmltest.xml"
xmldoc = System.Xml.XmlDocument()
xmldoc.Load(filename)
items = xmldoc.SelectNodes("data/items/item")
for item in items:
    print item.InnerText
1 Like

Steve,

Has their been any update to Rhinos implementation of IronPython with regard to xml libraries like SAX or DOM? I’m running SR7. I can use the System.XML approach as you have outlined but I’m a little more familiar with some of the other modules. Is this still on the cards for the future?

Will adding a ‘Module Search Path’ under the Tools|Options menu from the python editor to a full Iron Python install on my local hard drive cause issues with python finding conflicting duplicates of modules?

ie. C:/IronPython27 (default install of modules etc) and Rhino\Plug-ins\IronPython would have the rhino installed version.

We are not updating Ironpython versions with service releases of Rhino 5. Rhino 6 will include the latest version of Ironpython which will include a significant amount of support for additional modules. I’m not sure what the current state of IronPython is with respect to SAX/DOM support; you may be able to figure this out from their bugtracker

I would definitely recommend NOT tweaking your search path in the way you are describing. If you do, I don’t really know how to support any issues you run into.