Skip to content

XML Handler

Source

PLEXOS_NAMESPACE = 'http://tempuri.org/MasterDataSet.xsd'

Source

PLEXOS XML handler.

parse(cls, fpath: str | PathLike[str], namespace: str=PLEXOS_NAMESPACE, **kwargs: Any) -> 'XMLHandler'

Return XML instance from file requested.

create_table_element(self, rows: list[tuple[Any, ...]], column_types: dict[str, str], table_name: str) -> bool

Create XML elements for a given table.

get_records(self, element_enum: Schema, *elements: Iterable[str | int], rename_dict: dict[str, str] | None=None, **tag_elements: Any) -> list[XMLRecord]

Return XML text records without evaluating or coercing their contents.

Values are strings or None for empty elements. Schema-specific conversion belongs to the importer so numeric-looking text retains its spelling.

iter(self, element_type: Schema, *elements: Iterable[str | int], label: str | None=None, **tags: Any) -> Iterable[ET.Element]

Return elements from the XML based on the type.

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

Parameters

  • element_type Enum of the Schema wanted, e.g., Schema.Class, Schema.Objects.
  • *elements Sequence of ids, strings, or ints to get.
  • label 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

XML query match.

to_xml(self, fpath: str | PathLike[str]) -> bool

Save memory xml to file.

Full reference: XMLHandler.

Source

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

Parameters

  • element_name 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)

Returns

  • 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']"
xml_query(element_name: str, *tags: Any, **tag_elements: Any) -> str