5

Is it possible to auto-generate python class objects based on the hierarchical content of an XML file?
Let me explain a bit what I mean. Let's suppose I have an XML file that contains (for the sake of simplicity) the following:

<breakfast_menu>
   <food>
     <name>Belgian Waffles</name>
     <price>$5.95</price>
     <description>blah blah...etc...</description>
     <calories>650</calories>
   </food>
</breakfast_menu>

I like the way XML presents data and attributes, but I would like to use Python so I'm asking if there is a set of utilities that read the file above and create something like:

class breakfast_menu():
   food = food(self, name="Belgian Waffles", price="$5.95", description="blah blah...etc...", calories=650)

Is this something feasible? Can anyone suggest a way/tool to do that? Thank you in advance for your consideration.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Andrea
  • 735
  • 2
  • 8
  • 12
  • You can add attributes to a class at runtime in Python, so this should be possible. I don't know of a tool that will do this for you, however. – samfrances Oct 15 '12 at 15:47
  • 1
    possible duplicate of http://stackoverflow.com/questions/1072853/how-to-convert-xsd-to-python-class. I realize you're not using a schema here, but in order to generate a correct representation, it'll be hard to do without one imo. – FatalError Oct 15 '12 at 15:48
  • 1
    fwiw, there are tools I believfe that can "learn" the schema for you given a list of training documents. So, if that's what you have, you may want to start there. – FatalError Oct 15 '12 at 16:01
  • Perhaps if you gave us a reason why you are trying to do this, we could find a simpler solution. – Kari Roget Oct 15 '12 at 16:18
  • I apologize I'm not exactly the expert here. Let me try to explain: I believe that XML is a good way to organize data in a hierarchical manner. On top of that I'm dealing with already existing XML files that have been used for some time already. These contain values and attributes. Users already have tools to easily manipulate/view their data when stored in XML them. However if I could create the equivalent python class objects then I could use the power of an object oriented language like Python to (for example) create object instances, allow class derivation/composition, etc... – Andrea Oct 15 '12 at 17:14

2 Answers2

5

Parse it with some XML parser (ElementTree for example). Get content from each food tag into a Python dictionary. Unpack the dictionary when giving it to the function like this:

food(**my_dictionary)

If the dictionary contains something like my_dictionary = {'name':'Belgian Waffles', 'price':'$5.95'}, calling food(**my_dictionary) will be the same as calling food(name = 'Belgian Waffles', price = '$5.95'). For more information, look at Understanding kwargs in Python.

Community
  • 1
  • 1
Kari Roget
  • 74
  • 1
-1

Sounds like you need something like ElementTree XML API: http://docs.python.org/library/xml.etree.elementtree.html

Avi Meir
  • 980
  • 3
  • 11
  • 26
  • Looks like with etree it's possible to read/parse/modify/create/delete XML nodes, but is it possible to write out the (loosely) corresponding python class objects as .py file(s) as I provided in my example? – Andrea Oct 15 '12 at 15:38
  • Could you give a simple example, with some code and an explanation of what you're trying to achieve? – Avi Meir Oct 15 '12 at 15:43
  • I want to auto-generate python classes based on the content of an arbitrary XML file maintaining the hierarchy of the XML representation. – Andrea Oct 15 '12 at 15:46