module documentation

_xmllib.py - ZMS XML Parsing and Serialization Utilities

Provides functions for converting ZMS objects to/from XML representation, including support for complex data types (dictionaries, lists, blobs), multilingual attributes, and CDATA handling. Contains the XmlAttrBuilder parser for complex Python attributes and the XmlBuilder parser for custom XML structures.

License: GNU General Public License v2 or later, Organization: ZMS Publishing

Class XmlAttrBuilder XML parser for complex Python attributes (dictionaries, lists, blobs).
Class XmlBuilder XML parser for custom XML structures.
Exception ParseError Exception class to indicate XML parsing errors.
Function getAttrToXml Convert a single ZMS object attribute to its XML representation.
Function getObjPropertyToXml Convert a ZMS object property to XML, handling multilingual attributes.
Function getObjToXml Export a ZMS object and optionally its children to XML.
Function getText Extract text content from a list of DOM nodes.
Function getXmlType Return an XML type attribute string for a Python value.
Function getXmlTypeSaveValue Convert a string value to the appropriate Python type based on XML attrs.
Function parseString Parse an XML string into a DOM document.
Function serialize Serialize a DOM node to an XML string.
Function toCdata Wrap a string value in a CDATA section for safe XML embedding.
Function toXml Convert a Python value to its XML string representation.
Function xml_header Return an XML declaration header.
Function xmlInitObjProperty Initialize a ZMS object property with a value during XML import.
Function xmlNodeSet Retrieve node-set for given tag-name from dictionary of XML-Node-Structure.
Function xmlOnCharacterData Handle character data during XML import parsing.
Function xmlOnUnknownEndTag Handle unknown end tags during ZMS XML import.
Function xmlOnUnknownStartTag Handle unknown start tags during ZMS XML import.
Function xmlParse Parse arbitrary XML-Structure into dictionary.
Constant INDENTSTR Undocumented
def getAttrToXml(self, base_path, data2hex, obj_attr, REQUEST): (source)

Convert a single ZMS object attribute to its XML representation.

Parameters
selfZMS object
base_path:strBase path for resolving blob references
data2hex:boolWhether to hex-encode binary data
obj_attr:dictObject attribute definition dict
REQUESTZope request object
Returns
strXML string for the attribute value
def getObjPropertyToXml(self, REQUEST, base_path='', data2hex=False, obj_attr={}, multilang=True): (source)

Convert a ZMS object property to XML, handling multilingual attributes.

Parameters
selfZMS object
REQUESTZope request object
base_path:strBase path for resolving references
data2hex:boolWhether to hex-encode binary data
obj_attr:dictObject attribute definition dict
multilang:boolWhether to export all language variants
Returns
strXML string for the property
def getObjToXml(self, REQUEST, deep=True, base_path='', data2hex=False, multilang=True): (source)

Export a ZMS object and optionally its children to XML.

Parameters
selfZMS object to export
REQUESTZope request object
deep:boolWhether to recursively export child nodes
base_path:strBase path for resolving references
data2hex:boolWhether to hex-encode binary data
multilang:boolWhether to export all language variants
Returns
strXML string for the object tree
def getText(nodelist, encoding='utf-8'): (source)

Extract text content from a list of DOM nodes.

Parameters
nodelistDOM node or list of DOM nodes
encoding:strCharacter encoding (unused, kept for compatibility)
Returns
strConcatenated text content
def getXmlType(v): (source)

Return an XML type attribute string for a Python value.

Parameters
vPython value to determine XML type for
Returns
strXML type attribute (e.g. ' type="int"') or empty string
def getXmlTypeSaveValue(v, attrs): (source)

Convert a string value to the appropriate Python type based on XML attrs.

Parameters
v:strValue to convert (typically a string from XML parsing)
attrs:dictDictionary of XML attributes, expects 'type' key
Returns
Converted value (int, float, datetime, or stripped string)
def parseString(s): (source)

Parse an XML string into a DOM document.

Parameters
s:strXML string to parse
Returns
xml.dom.minidom.DocumentParsed DOM document
def serialize(node): (source)

Serialize a DOM node to an XML string.

Parameters
node:xml.dom.NodeDOM node to serialize
Returns
strXML string representation
def toCdata(self, s, xhtml=False): (source)

Wrap a string value in a CDATA section for safe XML embedding.

Returns the string as-is if it contains no special characters, otherwise wraps it in <![CDATA[...]]>.

Parameters
selfZMS context object
s:str or bytesString value to wrap
xhtml:boolWhether output is XHTML (unused)
Returns
strCDATA-wrapped string or original string
def toXml(self, value, indentlevel=0, xhtml=False, encoding='utf-8'): (source)

Convert a Python value to its XML string representation.

Handles images, files, dictionaries, lists, datetime tuples, numbers, and string values.

Parameters
selfZMS context object
valuePython value to convert
indentlevel:intCurrent XML indentation level
xhtml:boolWhether output is XHTML
encoding:strCharacter encoding
Returns
strXML string representation
def xml_header(encoding='utf-8'): (source)

Return an XML declaration header.

Parameters
encoding:strCharacter encoding for the header
Returns
strXML declaration string
def xmlInitObjProperty(self, key, value, lang=None): (source)

Initialize a ZMS object property with a value during XML import.

Handles type conversion for date, integer, float, and string fields.

Parameters
selfZMS object being imported
key:strProperty key/attribute name
valueProperty value to set
lang:str or NoneLanguage identifier (optional)
def xmlNodeSet(mNode, sTagName='', iDeep=0): (source)

Retrieve node-set for given tag-name from dictionary of XML-Node-Structure.

Returns
listList of dictionaries of XML-Structure.
def xmlOnCharacterData(self, sData, bInCData): (source)

Handle character data during XML import parsing.

Appends character data to the current tag on the tag stack.

Parameters
selfZMS object being imported
sData:strCharacter data string
bInCData:boolWhether data is inside a CDATA section
Returns
int1 to accept any character data
def xmlOnUnknownEndTag(self, sTagName): (source)

Handle unknown end tags during ZMS XML import.

Processes the completed element by popping it from the tag stack and handling its data according to element type (dict/list items, data blobs, language variants, conf-properties, or object attributes).

Parameters
selfZMS object being imported
sTagName:strXML element name
Returns
int1 to accept matching end tag, 0 on mismatch
def xmlOnUnknownStartTag(self, sTagName, dTagAttrs): (source)

Handle unknown start tags during ZMS XML import.

Manages the tag stack and value stack for nested XML elements including dictionaries, lists, data blobs, and language variants.

Parameters
selfZMS object being imported
sTagName:strXML element name
dTagAttrs:dictDictionary of element attributes
Returns
int1 to accept any unknown tag
def xmlParse(xml): (source)

Parse arbitrary XML-Structure into dictionary.

Parameters
xml:str or StringIOxml data
Returns
dictDictionary of XML-Structure.
INDENTSTR: str = (source)

Undocumented

Value
'  '