XML Handler

Plexos Input XML API.

class plexosdb.xml_handler.XMLHandler(fpath=None, namespace='http://tempuri.org/MasterDataSet.xsd', in_memory=True, initialize=False)

Bases: object

PLEXOS XML handler.

Initialize XML handler for PLEXOS datasets.

Parameters:
  • fpath (str or PathLike, optional) – Path to an existing XML file to load.

  • namespace (str, optional) – XML namespace to use when serializing.

  • in_memory (bool, optional) – Store element caches in memory for faster lookups.

  • initialize (bool, optional) – Build an empty XML tree instead of parsing a file.

Return type:

None

classmethod parse(fpath, namespace='http://tempuri.org/MasterDataSet.xsd', **kwargs)

Return XML instance from file requested.

Parameters:
  • fpath (str | PathLike[str])

  • namespace (str)

  • kwargs (Any)

Return type:

XMLHandler

create_table_element(rows, column_types, table_name)

Create XML elements for a given table.

Parameters:
  • rows (list[tuple[Any, ...]])

  • column_types (dict[str, str])

  • table_name (str)

Return type:

bool

get_records(element_enum, *elements, rename_dict=None, **tag_elements)

Return a given element(s) as list of dictionaries.

Parameters:
  • element_enum (Schema)

  • elements (Iterable[str | int])

  • rename_dict (dict[str, str] | None)

  • tag_elements (Any)

Return type:

list[dict[str, Any]]

iter(element_type, *elements, label=None, **tags)

Return elements from the XML based on the type.

This functions serves as a low-level query to the XML file.

Parameters:
  • element_type (Schema) – Enum of the Schema wanted, e.g., Schema.Class, Schema.Objects.

  • *elements (Iterable[str | int]) – Sequence of ids, strings, or ints to get.

  • label (str | None) – XML child label to extract. Defaults to Schema[elementy_type].label.

  • **tags (Any) – Additional key: value pairs to match the XML, e.g., kwargs = {“class_id”: 1}.

Return type:

XML query match.

to_xml(fpath)

Save memory xml to file.

Parameters:

fpath (str | PathLike[str])

Return type:

bool

plexosdb.xml_handler.xml_query(element_name, *tags, **tag_elements)

Construct XPath query for extracting data from a XML with no namespace.

Parameters:
  • element_name (str) – String that matches the desired element

  • *tags (Any) – Tag names to filter

  • **kwargs – Tag name and value child of the element. (E.g., class_id=2)

  • tag_elements (Any)

Return type:

XPath query string constructed based on the provided conditions.

Examples

A simple example for one condition:

>>> query_string = xml_query("t_object", class_id="2")
>>> print(query_string)
".//t_object[class_id='2']"

For multiple condition:

>>> query_string = xml_query("t_object", class_id="2", enum_id="1")
>>> print(query_string)
".//t_object[class_id='2'][enum_id='1']"