Writing XML files with RhinoPython?

Hi…,

is there an example to write XML files with RhinoPython?

Thank you very much

Michael

www.flexiCAD.com

I don’t think there is anything special about XML writing in RhinoPython. You should be able to use the default XML tools in the Python standard library or an external package.

See https://wiki.python.org/moin/PythonXml

Hi menno,

thank you for this link. I am new to RhinoPython. I’ve got copied a XML example to RhinoPython with this line

from elementtree.SimpleXMLWriter import XMLWriter

but got at once an error message “no module named elementtree.SimpleXMLWriter”

Any ideas?

Thanks

Michael

Hey Michael, you might as well take a look into this thread.

c.

Hi clement and @stevebaer ,

yes, I’ve seen this. Steve wrote there will be better XML Python support in SR6. I’ve tried Steves example, but I didn’t get it to run.

Hm?

Thanks

Michael

www.flexiCAD.com

Hi Michael, you´ll need to add indents (4 spaces or 1x tab) below the definition if this error comes up. To do this, select the content from line 6 to 11 and press tab key once. Also line 14 needs one indent (4 spaces or 1x tab), does it work ?


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

def XMLTest():
    filename = r"C:\xmltest.xml"  
    xmldoc = System.Xml.XmlDocument()  
    xmldoc.Load(filename)  
    items = xmldoc.SelectNodes("data/items/item")  
    for item in items:  
        print item.InnerText

if __name__=="__main__":
    XMLTest()

c.

Hi clement,

first of all, thank you for your time. I didn’t now anything about indents, I know brackets in all kind of forms from C++, C#… :wink:

OK, this problem is solved, but it still doesn’t run. Hm?

Michael

Hi Michael,

this seems to work here, make shure to not save the xml file in the root directory and note that uppercase and lowercase counts:

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

def XMLTest():
    filename = r"C:\Users\Clement\Desktop\XMLTest.xml"  
    xmldoc = System.Xml.XmlDocument()  
    xmldoc.Load(filename)  
    items = xmldoc.SelectNodes("data/items/item")  
    for item in items:  
        print item.InnerText

if __name__=="__main__":
    XMLTest()

the result should look like this:

…oh yeah, and not to forget semicolons at the end of each line :blush:

c.

Hi clement,

you helped me out, now I’ve got it to work, silly me; :wink:

I hope I can now program something usefull.

Thanks a lot and cheers

Michael

Hi XML-fans,

I am new to XML and Python scripting in Rhino, but it is easier than I thought. Here my working example:

import rhinoscriptsyntax as rs
# xml import
import clr
clr.AddReference("System.Xml")
import System.Xml

def XMLExport():
    #prompt the user to specify a file name
    filter = "XML File (*.xml)|*.xml|All files (*.*)|*.*||"
    filename = rs.SaveFileName("Save XML file As", filter)
    if not filename: return
    
    xmlWriter = System.Xml.XmlTextWriter(filename, None)
    xmlWriter.WriteStartDocument()
    xmlWriter.WriteStartElement("car")
    xmlWriter.WriteElementString("name", "Jaguar")
    xmlWriter.WriteEndElement()
    xmlWriter.WriteEndDocument()
    xmlWriter.Close()
    
if __name__=="__main__":
    XMLExport()

Thanks and happy times

Michael

2 Likes