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

XML Attributes

XML elements can convey data in two distinct ways: named attributes within the XML tag, and child data (string data or additional XML tags) between the element's open and close tags.

Named attributes should be added in XMLObjApp's middle section by clicking the Add Attr. button. Add one entry for each named attribute that can appear within the given tag. Attributes may be removed with the Remove button, but be careful! There is no undo.

Figure 3.3. Defining Named Attributes

Defining Named Attributes

Select an appropriate type and default for each attribute. A variety of types are provided in the pull-down, but you can also enter your own function or class by typing the name in the combo-box. See Manually Editing Your Parser for more on entering your own code.

If an attributes does not have a default value, you can pull down values of (required) or None. Choosing (required) will require the attribute to be present in the tag, or else a TypeError exception will be thrown during the parsing process.

The above example will parse the XML tag

    <Fruit Name="oranges" Quantity="10" Price="0.59" Units="per pound">

into the following object:

    Obj = Fruit()
    Obj.Name     = "oranges"
    Obj.Quantity = 10
    Obj.Price    = 0.59
    Obj.Units    = "per pound"

Note

Named attributes are always saved as object members of the same name.

UniqueID and ReferenceID

XMLObj provides two special types for dealing with unique identifiers – UniqueID to hold them and ReferenceID to cross-reference them.

Suppose you wanted to represent a set of objects that don't fit cleanly into XML's single-parent/multiple-children hierarchy:

Figure 3.4. Family Tree

Family Tree

You could do it with the following XML:

Example 3.3. Family Tree XML

<Family>
    <Member Name="Abe" DOB="3/31/42" />
    <Member Name="Betty" DOB="2/4/49" />
    <Member Name="Cathy" DOB="12/2/78" />
    <Member Name="Dan" Father="Abe" Mother="Betty" DOB="6/12/73" />
    <Member Name="Edith" Father="Abe" Mother="Betty" DOB="8/30/80" />
    <Member Name="Frank" DOB="11/4/70" />
    <Member Name="George" Father="Dan" Mother="Cathy" DOB="5/13/94" />
    <Member Name="Harold" Father="Dan" Mother="Cathy" DOB="7/1/97" />
    <Member Name="Irwin" Father="Frank" Mother="Edith" DOB="10/31/01" />
    <Member Name="Janet" Father="Frank" Mother="Edith" DOB="1/17/03" />
</Family>

By configuring the Name attribute as a UniqueID type and the Father and Mother attributes as ReferenceID type, we can make it easy to cross-reference between them.

Figure 3.5. Family Tree Configuration

Family Tree Configuration

Example 3.4. Accessing Elements with ReferenceID

>>> import XMLObject, tree
>>> Tree = XMLObject.Parse("tree.xml", "Family", tree.Family)
>>> Tree.Member["Janet"].DOB
'1/17/03'
>>> Tree.Member["Janet"].Mother().DOB
'8/30/80'
>>> Tree.Member["Janet"].Mother().Father().DOB
'3/31/42'

Note

Unless you have elements that need to cross-reference to other elements, then there is probably no need to use UniqueID. See ReferenceID.

More information on the use of ReferenceID types may be found in ReferenceID.

Constraining XML Attributes

Attribute values may be constrained somewhat by selecting the appropriate type and/or by setting an attribute's default to (required). However, this is not always sufficient. Sometimes you need to:

  • insure that a string attribute is any one value from a finite list of options

  • insure that a numeric attribute falls within an "acceptable" range

  • allow a tag to define one attribute or another, but not both

Although XMLObjApp does not let you enter constraints such as these directly, you can constrain XML attributes by manually editing the parser source.