| 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 button. Add one entry for each named attribute that can appear within the given tag. Attributes may be removed with the button, but be careful! There is no undo. 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" NoteNamed attributes are always saved as object members of the same name. 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: 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. 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' NoteUnless 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. 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:
Although XMLObjApp does not let you enter constraints such as these directly, you can constrain XML attributes by manually editing the parser source. |