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.

5
  • 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. Commented Oct 15, 2012 at 15:47
  • 1
    possible duplicate of stackoverflow.com/questions/1072853/…. 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. Commented Oct 15, 2012 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. Commented Oct 15, 2012 at 16:01
  • Perhaps if you gave us a reason why you are trying to do this, we could find a simpler solution. Commented Oct 15, 2012 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... Commented Oct 15, 2012 at 17:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Using a dict is simpler than a class if inheritance isn't required.
How does this handle XML attributes? For example: <person name='John'/>
-1

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

3 Comments

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?
Could you give a simple example, with some code and an explanation of what you're trying to achieve?
I want to auto-generate python classes based on the content of an arbitrary XML file maintaining the hierarchy of the XML representation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.