Acspy.Util.XmlObjectifier
index
/alma/ACS-2016.6/ACSSW/lib/python/site-packages/Acspy/Util/XmlObjectifier.py

This module is used to create native Python objects representing an XML document
rendering the elements into a hierarchical tree. Name spaces can optionally be
mapped into the element names by specifying 'mapNameSpaces = 1'. Leading
characters can be omitted in the name space mapping using the 'skipChars'
argument.
 
Characters that are not allowed in Python names ('.', '-', '/', ':') are mapped
to '_'.
 
The resulting Python object can be modified and serialized into XML again using
the 'writexml' method.
 
Example usage:
 
import XmlObjectifier
xmlObject = XmlObjectifier.XmlObject(xmlString = <XML string>,
                                     skipChars = <string>)
 
or
 
xmlObject = XmlObjectifier.XmlObject(fileName = '<file name>',
                                     skipChars = <string>)
 
This example XML document:
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- edited with XMLSPY v5 U (http://www.xmlspy.com) by D. Muders (MPIfR) -->
<TelCalResult xmlns="Alma/TelCalResult"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="Alma/TelCalResult/TelCalResult-single.xsd">
        <TelCalResultEntity entityId="2718281828" entityIdEncrypted=""
            entityVersion="0.1"/>
        <TelCalResultRef entityId="2718281827"/>
        <SchedBlockRef entityId="31415926534"/>
        <TelCalResultDetail>
                <ResultKind>FocusOffset</ResultKind>
                <ScanID>1789</ScanID>
                <NumAntennas>2</NumAntennas>
                <AntennaID>64</AntennaID>
                <AntennaID>42</AntennaID>
                <FocusOffset>-0.25</FocusOffset>
                <FocusOffset>-0.34</FocusOffset>
        </TelCalResultDetail>
</TelCalResult>
 
can then be navigated in the Python object like this:
 
#!/usr/bin/env python
 
import XmlObjectifier
 
def printInfo(focusResult):
    scanID = focusResult.TelCalResult.TelCalResultDetail.ScanID.getValue()
    kind = focusResult.TelCalResult.TelCalResultDetail.ResultKind.getValue()
    numAnts = focusResult.TelCalResult.TelCalResultDetail.NumAntennas.getValue()
 
    print 'This is a %s result entity. Scan ID: %d. Number of antennas: %d' %         (kind, scanID, numAnts)
    print
 
    for ant in xrange(numAnts):
        antID =             focusResult.TelCalResult.TelCalResultDetail.AntennaID[ant].getValue()
        focusOffset =             focusResult.TelCalResult.TelCalResultDetail.FocusOffset[ant].getValue()
        print 'Antenna #%d focus offset: %.1f' % (antID, focusOffset)
 
# Objectify XML
focusResult = XmlObjectifier.XmlObject(fileName = 'FocusResult.xml')
 
# Print object summary
print 'Original focus result:
'
printInfo(focusResult)
 
# Optionally modify elements
focusResult.TelCalResult.TelCalResultDetail.ScanID.setValue(1790)
focusResult.TelCalResult.TelCalResultDetail.AntennaID[0].setValue(24)
focusResult.TelCalResult.TelCalResultDetail.FocusOffset[0].setValue(0.3)
focusResult.TelCalResult.TelCalResultDetail.AntennaID[1].setValue(25)
focusResult.TelCalResult.TelCalResultDetail.FocusOffset[1].setValue(0.5)
 
# Print object summary
print '
 
New focus result:
'
printInfo(focusResult)
 
# Write XML to a new file
f = open('FocusResultNew.xml', 'w+')
focusResult.writexml(f, '')
f.close()

 
Modules
       
xml.dom.minidom

 
Classes
       
exceptions.Exception(exceptions.BaseException)
XmlObjectifierError
xml.dom.minidom.Document(xml.dom.minidom.Node, xml.dom.xmlbuilder.DocumentLS)
XmlObject
xml.dom.minidom.Element(xml.dom.minidom.Node)
XmlElement

 
class XmlElement(xml.dom.minidom.Element)
    Creates an object representing an XML tag/element with all of its content.
 
 
Method resolution order:
XmlElement
xml.dom.minidom.Element
xml.dom.minidom.Node
xml.dom.Node
xml.dom.minicompat.GetattrMagic

Methods defined here:
__init__(self, element, mapNameSpaces, nameSpaceMapping, skipChars)
getAttribute(self, name)
Overwrites the inherited method and returns a value of the right type.
getValue(self)
Returns the included TEXT, if present.
setValue(self, value)
Sets the included TEXT.

Methods inherited from xml.dom.minidom.Element:
__repr__(self)
getAttributeNS(self, namespaceURI, localName)
getAttributeNode(self, attrname)
getAttributeNodeNS(self, namespaceURI, localName)
getElementsByTagName(self, name)
getElementsByTagNameNS(self, namespaceURI, localName)
hasAttribute(self, name)
hasAttributeNS(self, namespaceURI, localName)
hasAttributes(self)
removeAttribute(self, name)
removeAttributeNS(self, namespaceURI, localName)
removeAttributeNode(self, node)
removeAttributeNodeNS = removeAttributeNode(self, node)
setAttribute(self, attname, value)
setAttributeNS(self, namespaceURI, qualifiedName, value)
setAttributeNode(self, attr)
setAttributeNodeNS = setAttributeNode(self, attr)
setIdAttribute(self, name)
setIdAttributeNS(self, namespaceURI, localName)
setIdAttributeNode(self, idAttr)
unlink(self)
writexml(self, writer, indent='', addindent='', newl='')

Data descriptors inherited from xml.dom.minidom.Element:
attributes
NamedNodeMap of attributes on the element.
localName
Namespace-local name of this element.

Data and other attributes inherited from xml.dom.minidom.Element:
nodeType = 1
nodeValue = None
schemaType = <TypeInfo None>

Methods inherited from xml.dom.minidom.Node:
__nonzero__(self)
appendChild(self, node)
cloneNode(self, deep)
getInterface(self, feature)
getUserData(self, key)
hasChildNodes(self)
insertBefore(self, newChild, refChild)
isSameNode(self, other)
isSupported(self, feature, version)
normalize(self)
removeChild(self, oldChild)
replaceChild(self, newChild, oldChild)
setUserData(self, key, data, handler)
toprettyxml(self, indent='\t', newl='\n', encoding=None)
toxml(self, encoding=None)

Data descriptors inherited from xml.dom.minidom.Node:
firstChild
First child node, or None.
lastChild
Last child node, or None.

Data and other attributes inherited from xml.dom.minidom.Node:
namespaceURI = None
nextSibling = None
ownerDocument = None
parentNode = None
prefix = None
previousSibling = None

Data and other attributes inherited from xml.dom.Node:
ATTRIBUTE_NODE = 2
CDATA_SECTION_NODE = 4
COMMENT_NODE = 8
DOCUMENT_FRAGMENT_NODE = 11
DOCUMENT_NODE = 9
DOCUMENT_TYPE_NODE = 10
ELEMENT_NODE = 1
ENTITY_NODE = 6
ENTITY_REFERENCE_NODE = 5
NOTATION_NODE = 12
PROCESSING_INSTRUCTION_NODE = 7
TEXT_NODE = 3
TREE_POSITION_ANCESTOR = 4
TREE_POSITION_DESCENDENT = 8
TREE_POSITION_DISCONNECTED = 0
TREE_POSITION_EQUIVALENT = 16
TREE_POSITION_FOLLOWING = 2
TREE_POSITION_PRECEDING = 1
TREE_POSITION_SAME_NODE = 32

 
class XmlObject(xml.dom.minidom.Document)
    Creates an object representing the XML document wich is to be objectified.
The XML string passed to the constructor is preferred over any specified 
XML file.
 
Optionally the name space mapping can be turned on by passing mapNameSpaces = 1.
 
Leading characters in the name space definitions can be skipped in the mapping
by passing the optional "skipChars" argument.
 
 
Method resolution order:
XmlObject
xml.dom.minidom.Document
xml.dom.minidom.Node
xml.dom.Node
xml.dom.minicompat.GetattrMagic
xml.dom.xmlbuilder.DocumentLS

Methods defined here:
__init__(self, xmlString=None, fileName=None, skipChars='', mapNameSpaces=0)

Methods inherited from xml.dom.minidom.Document:
appendChild(self, node)
cloneNode(self, deep)
createAttribute(self, qName)
createAttributeNS(self, namespaceURI, qualifiedName)
createCDATASection(self, data)
createComment(self, data)
createDocumentFragment(self)
createElement(self, tagName)
createElementNS(self, namespaceURI, qualifiedName)
createProcessingInstruction(self, target, data)
createTextNode(self, data)
getElementById(self, id)
getElementsByTagName(self, name)
getElementsByTagNameNS(self, namespaceURI, localName)
importNode(self, node, deep)
isSupported(self, feature, version)
removeChild(self, oldChild)
renameNode(self, n, namespaceURI, name)
unlink(self)
writexml(self, writer, indent='', addindent='', newl='', encoding=None)

Data descriptors inherited from xml.dom.minidom.Document:
documentElement
Top-level element of this document.

Data and other attributes inherited from xml.dom.minidom.Document:
actualEncoding = None
attributes = None
doctype = None
documentURI = None
encoding = None
errorHandler = None
implementation = <xml.dom.minidom.DOMImplementation instance>
nextSibling = None
nodeName = '#document'
nodeType = 9
nodeValue = None
parentNode = None
previousSibling = None
standalone = None
strictErrorChecking = False
version = None

Methods inherited from xml.dom.minidom.Node:
__nonzero__(self)
getInterface(self, feature)
getUserData(self, key)
hasAttributes(self)
hasChildNodes(self)
insertBefore(self, newChild, refChild)
isSameNode(self, other)
normalize(self)
replaceChild(self, newChild, oldChild)
setUserData(self, key, data, handler)
toprettyxml(self, indent='\t', newl='\n', encoding=None)
toxml(self, encoding=None)

Data descriptors inherited from xml.dom.minidom.Node:
firstChild
First child node, or None.
lastChild
Last child node, or None.
localName
Namespace-local name of this node.

Data and other attributes inherited from xml.dom.minidom.Node:
namespaceURI = None
ownerDocument = None
prefix = None

Data and other attributes inherited from xml.dom.Node:
ATTRIBUTE_NODE = 2
CDATA_SECTION_NODE = 4
COMMENT_NODE = 8
DOCUMENT_FRAGMENT_NODE = 11
DOCUMENT_NODE = 9
DOCUMENT_TYPE_NODE = 10
ELEMENT_NODE = 1
ENTITY_NODE = 6
ENTITY_REFERENCE_NODE = 5
NOTATION_NODE = 12
PROCESSING_INSTRUCTION_NODE = 7
TEXT_NODE = 3
TREE_POSITION_ANCESTOR = 4
TREE_POSITION_DESCENDENT = 8
TREE_POSITION_DISCONNECTED = 0
TREE_POSITION_EQUIVALENT = 16
TREE_POSITION_FOLLOWING = 2
TREE_POSITION_PRECEDING = 1
TREE_POSITION_SAME_NODE = 32

Methods inherited from xml.dom.xmlbuilder.DocumentLS:
abort(self)
load(self, uri)
loadXML(self, source)
saveXML(self, snode)

Data and other attributes inherited from xml.dom.xmlbuilder.DocumentLS:
async = False

 
class XmlObjectifierError(exceptions.Exception)
    
Method resolution order:
XmlObjectifierError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Methods defined here:
__init__(self, msg, code=None)
__str__(self)

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__unicode__(...)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message

 
Functions
       
castType(value)