pipeline.extern.XmlObjectifier¶
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 python3
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))
- for ant in range(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()
Functions
|
Classes
|
Creates an object representing an XML tag/element with all of its content. |
|
Creates an object representing the XML document wich is to be objectified. |
Exceptions
|