Asking for Help: Is there a way to create a file object from text without touching the file system?
I want to use ElementTree to parse a text object, but the ElementTree.parse function looks like it only takes file objects. I'm doing this all from within Blender, and don't want to touch the filesystem - so is there a way to create a file object from text (to pass to parse) without touching the file system? I'd also like to go the other way, create a text object from an ElementTree object.
The StringIO module provides support for treating strings like file objects. Since there's a faster version of the module, the idiom to use to access the all-important StringIO class (yes, it has the same name) is as follows:
In fact, for the exact problem you're having, the example "21 lines: XML/HTML parsing (using Python 2.5 or third-party library)" from the SimplePrograms page should at least help you with the parsing problem. For serialising ("going the other way"), you probably want to create a StringIO object and then pass it to the appropriate ElementTree node method.