scenario.xmlutils module

XML DOM utils.

class Xml

Bases: object

Because:

  1. there seems to be no obvious portable choice for parsing and writing XML in Python (see https://stackoverflow.com/questions/1912434/how-to-parse-xml-and-count-instances-of-a-particular-node-attribute),

  2. standard libraries such as xml.dom.minidom are sometimes untyped or partially untyped,

let’s define a wrapper that gives us the opportunity to abstract the final library used and work around typing issues.

class Document

Bases: object

XML document.

__init__()

Instanciates an XML document, either for reading or writing.

_xml_doc

Underlying library document reference.

property root
Returns:

Retrieves the root node of the document.

static read(path)

Reads from an XML file.

Parameters:

path – File to read from.

Returns:

XML document read from the file.

write(path)

Writes the document into a file.

Parameters:

path – File to write to.

createnode(tag_name)

Create a node with the given tag name.

Parameters:

tag_name – Tag name.

Returns:

New node.

createtextnode(text)

Create a text node.

Parameters:

text – Initial text for the new node.

Returns:

New text node.

class INode

Bases: ABC

Abstract interface for regular nodes and text nodes.

_abc_impl = <_abc_data object>
class Node

Bases: INode

Regular XML node.

__init__(xml_element)
Parameters:

xml_element – Underlying library node reference.

_xml_element

Underlying library node reference.

property tag_name
Returns:

Tag name of the node.

hasattr(name)

Tells whether the node has an attribute of the given name.

Parameters:

name – Attribute name.

Returns:

True when the node has an attribute of the given name, False otherwise.

getattr(name)

Retrives the attribute value of the given name.

Parameters:

name – Attribute name.

Returns:

Attribute value, or possibly an empty string if the attribute does not exist.

setattr(name, value)

Set an attribute.

Parameters:
  • name – Attribute name.

  • value – Attribute value.

Returns:

self

getchildren(tag_name)

Retrieves direct children with the given tag name.

Parameters:

tag_name – Children tag name.

Returns:

List of children nodes.

gettextnodes()

Retrieves direct children text nodes.

Returns:

List of children text nodes.

appendchild(child)

Adds a child to the node.

Parameters:

child – New node or text node to set as a child.

Returns:

The child just added.

_abc_impl = <_abc_data object>
class TextNode

Bases: INode

Text node.

__init__(xml_text)
Parameters:

xml_text – Underlying library text node reference.

_xml_text

Underlying library text node reference.

property data

Text content.

append(data)

Adds some text to the text node.

Parameters:

data – Additional text.

_abc_impl = <_abc_data object>