ProjectPythonSourceForge
Welcome
Introduction
Suitability
Future Plans
Setup
Downloading XMLObject
Installing XMLObject
XMLObjApp
The XMLObjApp Application
Classes
Special Attributes
XML Attributes
Child Tags
File Menu
Miscellaneous Operation Notes
Unicode and ASCII Strings
Manually Editing Your Parser
Setting a class' Parent
_end_init Constructor Completion
Outputting XML
XMLObject
XMLObject -- XML to Object Conversion
Stack -- Tracks the Document Hierarchy
All Docs on One Page

Manually Editing Your Parser

Setting a class' Parent

Although XMLObject automatically generates a Python parser for you, you may wish to edit the parser to enhance its functionality. One time that you'll certainly want to do this is when you derive the parser classes from other classes.

Consider the painfully simple example:

Figure 4.1. Parent class Configuration

Parent class Configuration

This will create the following parser:

Example 4.1. Parent class Parser - 1

# vvv  Generated code, do not modify  vvv
import XMLObject
# ^^^  Generated code, do not modify  ^^^

# vvv  Generated code, do not modify  vvv
class Root(Parent):
    ChildSpec = XMLObject.ChildClass({}, "")
    def __init__(self, Attr, XMLStack=XMLObject.Stack()):
        self.Attr = str(Attr)
# ^^^  Generated code, do not modify  ^^^

This code won't load, of course, since we haven't yet defined the Parent class just yet. We can do this by editing the code and either entering the class into the module or by importing it with a:

from ParentModule import Parent

For now, we'll just add code directly into the parser:

Example 4.2. Parent class Parser - 2

# vvv  Generated code, do not modify  vvv
import XMLObject
# ^^^  Generated code, do not modify  ^^^

class Parent:
    def __init__(self):
        self.Var = 5

# vvv  Generated code, do not modify  vvv
class Root(Parent):
    ChildSpec = XMLObject.ChildClass({}, "")
    def __init__(self, Attr, XMLStack=XMLObject.Stack()):
        self.Attr = str(Attr)
# ^^^  Generated code, do not modify  ^^^
        Parent.__init__(self)

Note how I've added the Parent class and the Parent class construction without modifying any code within the blocks marked "do not modify". This is important.

Modifying the code within these blocks can still create valid code, but you may no longer be able to reload the code with XMLObjApp. Worse, the application may load the code, but then save changes to these blocks without any warnings. Just don't do it.

You can also add member functions to classes, as long as the code you add is not within the "do not modify" blocks.

_end_init Constructor Completion

XMLObject processes the XML file from top to bottom, and performs the following actions on each element in order:

  1. class instantiation (__init__ member)

  2. child processing

  3. _end_init construction completion (if present)

  4. member reduction (if required)

_end_init accepts no parameters. It gives you a chance to do any initialization that cannot happen until after the element's children have been processed.