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.
- Parameters:
fpath (str | PathLike | None)
namespace (str)
in_memory (bool)
initialize (bool)
- classmethod parse(fpath, namespace='http://tempuri.org/MasterDataSet.xsd', **kwargs)¶
Return XML instance from file requested.
- Parameters:
fpath (str | PathLike)
namespace (str)
- Return type:
- create_table_element(rows, column_types, table_name)¶
Create XML elements for a given table.
- Parameters:
rows (list[tuple])
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 | None)
- Return type:
list[dict]
- 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 – 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)
- 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 – Tag names to filter
**kwargs – Tag name and value child of the element. (E.g., class_id=2)
- 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']"