| Each class may include two optional, special attributes. By default, these are called _parent and _root. The _parent attribute provides access to the parent tag instance and the _root attribute provides access to the root of the XML structure. Let's illustrate this relationship with a very simple piece of XML which describes my home town: Example 3.1. Sample Location XML
<Country Name="USA">
<State Name="Missouri">
<City Name="Columbia" />
</State>
</Country>
In the above example, the City object's _parent attribute refers to the State object. The City object's _root attribute refers to the Country object. Let's explore this with a little code: Example 3.2. Exploring _parent and _root
>>> import XMLObject, city
>>> Country = XMLObject.Parse("city.xml", "Country",
... city.Country)
>>> Country.Name
'USA'
>>> Country.State.Name
'Missouri'
>>> Country.State.City.Name
'Columbia'
>>> Country.State.City._parent.Name
'Missouri'
>>> Country.State.City._root.Name
'USA'
|