def _structure_init(self, _dataset):
"""Initializes the XML structure that data is to be applied to."""
print("XpathDataset._structure_init")
super(XpathDataset, self)._structure_init(_dataset)
# Parse important information data from XPath
_root_node_name, self._structure_row_node_name, _parent_xpath = self._structure_parse_root_path(self.rows_xpath)
# If the structure already loaded?
if self._structure_row_node_parent is None:
# If not try to load, or create file.
import os
if os.path.exists(make_path_absolute(self.filename, self._base_path)):
try:
self.load(_add_node_ref=True)
except Exception as e:
raise Exception("XpathDataset.save - error parsing " + self.xpath_data_format + " file : " + str(e))
else:
# Create a tree with root node based on the first
if _root_node_name != "":
# noinspection PyUnusedLocal
if self.encoding:
_encoding = self.encoding
else:
_encoding = "UTF-8"
# TODO: Check why this is done, _tree isn't used
# noinspection PyUnusedLocal
_tree = etree.parse(io.StringIO("<?xml version='1.0' ?>\n<" + _root_node_name + "/>"))
else:
raise Exception("XpathDataset.save - rows_xpath(" + str(
self.rows_xpath) + ") must be absolute and have at least the name of the root node. " +
"Example: \"/root_node\" ")
# If the structure there yet? It could be an XML file with only a top node.
if self._structure_row_node_parent is None:
# If not existing, create a node structure up to the parent or the row nodes
# from the information in the xpath.
self._structure_top_node = self._structure_create_xpath_nodes(self._structure_top_node, self.rows_xpath)
评论列表
文章目录