Package fcp :: Module xmlobject
[hide private]
[frames] | no frames]

Module xmlobject

source code

Allows XML files to be operated on like Python objects.

Features:

Example XML file:

   <?xml version="1.0" encoding="UTF-8"?>
   <rapsheets>
    <person name="John Smith" age="42">
       <!-- John Smith has an appeal in process against his last conviction -->
       <crime name="Armed robbery" date="March 11, 1994"/>
       <crime name="Aggravated burglary" date="June 9, 2001"/>
    </person>
    <person name="Mary Jones" age="33">
       <crime name="Prostitution" date="January 8, 1997"/>
       <crime name="Selling heroin" date="September 4, 2002"/>
       <crime name="Manslaughter" date="December 21, 2004"/>
    </person>
   </rapsheets>

Example usage:

   >>> from xmlobject import XMLFile
   
   >>> x = XMLFile(path="sample.xml)

   >>> print x
   <xmlobj.XMLFile instance at 0xb7ccc52c>

   >>> print x.root
   <XMLNode: rapsheets>

   >>> print x.root._children
   [<XMLNode: text>, <XMLNode: person>, <XMLNode: text>,
    <XMLNode: person>, <XMLNode: text>]

   >>> print x.root.person
   [<XMLNode: person>, <XMLNode: person>]

   >>> print x.root.person[0].name
   John Smith

   >>> john = x.root.person[0]
   
   >>> john.height = 184

   >>> c = john._addNode("crime")

   >>> c.name = "Grand Theft Auto"
   
   >>> c.date = "4 May, 2005"

   >>> print x.toxml()
   <?xml version="1.0" ?>
   <rapsheets>
    <person age="42" height="184" name="John Smith">
       <!-- John Smith has an appeal in process against his last conviction -->
       <crime date="March 11, 1994" name="Armed robbery"/>
       <crime date="June 9, 2001" name="Aggravated burglary"/>
    <crime date="4 May, 2005" name="Grand Theft Auto"/></person>
    <person age="33" name="Mary Jones">
       <crime date="January 8, 1997" name="Prostitution"/>
       <crime date="September 4, 2002" name="Selling heroin"/>
       <crime date="December 21, 2004" name="Manslaughter"/>
    </person>
   </rapsheets>

   >>>
Classes [hide private]
  MissingRootTag
root tag name was not given
  InvalidXML
failed to parse XML input
  CannotSave
unable to save
  InvalidNode
not a valid minidom node
  XMLFile
Allows an xml file to be viewed and operated on as a python object.
  XMLNode
This is the workhorse for the xml object interface
Variables [hide private]
  impl = getDOMImplementation()
  __package__ = 'fcp'