YAML data to DOM: use key as item element name + remove wrapper v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 28 Oct 2020 00:05:29 +0100
branchv_0
changeset 18 737c507a3e32
parent 17 75c6685cceb9
child 19 aabdab3e05a0
YAML data to DOM: use key as item element name + remove wrapper
src/XMLDocumentConstructor.h
--- a/src/XMLDocumentConstructor.h	Tue Oct 27 22:39:50 2020 +0100
+++ b/src/XMLDocumentConstructor.h	Wed Oct 28 00:05:29 2020 +0100
@@ -80,6 +80,7 @@
 	void process() {
 		current = parser->get_document()->create_root_node("yaml");
 		mode.push_back(Mode::ROOT);
+		std::string itemName;
 
 		while (true) {
 			yaml_event_t event;
@@ -110,7 +111,7 @@
 			} else if (event.type == YAML_SCALAR_EVENT) {
 				if (mode.back() == Mode::SEQUENCE) {
 					std::cerr << "YAML_SCALAR_EVENT: Mode::SEQUENCE: " << event.data.scalar.value << std::endl;
-					current->add_child("item")->add_child_text(y2x(event.data.scalar.value));
+					current->add_child(itemName)->add_child_text(y2x(event.data.scalar.value));
 				} else if (mode.back() == Mode::MAPPING) {
 					std::cerr << "YAML_SCALAR_EVENT: Mode::MAPPING: " << event.data.scalar.value << std::endl;
 					current = current->add_child(y2xname(event.data.scalar.value));
@@ -126,17 +127,26 @@
 
 			} else if (event.type == YAML_SEQUENCE_START_EVENT) {
 				std::cerr << "YAML_SEQUENCE_START_EVENT" << std::endl;
+				xmlpp::Element* parent = current->get_parent();
+				if (parent) {
+					itemName = current->get_name();
+					parent->remove_child(current);
+					current = parent;
+				} else {
+					itemName = "item";
+				}
 				if (mode.back() == Mode::MAP_KEY) mode.pop_back();
 				mode.push_back(Mode::SEQUENCE);
 			} else if (event.type == YAML_SEQUENCE_END_EVENT) {
 				std::cerr << "YAML_SEQUENCE_END_EVENT" << std::endl;
-				current = parentOrSelf(current);
 				mode.pop_back(); // TODO: assert sequence?
 			} else if (event.type == YAML_MAPPING_START_EVENT) {
 
-				if (mode.back() == Mode::SEQUENCE) {
+				if (mode.back() == Mode::ROOT) {
+					std::cerr << "YAML_MAPPING_START_EVENT: Mode:ROOT" << std::endl;
+				} else if (mode.back() == Mode::SEQUENCE) {
 					std::cerr << "YAML_MAPPING_START_EVENT: Mode::SEQUENCE" << std::endl;
-					current = current->add_child("item");
+					current = current->add_child(itemName);
 				} else if (mode.back() == Mode::MAP_KEY) {
 					std::cerr << "YAML_MAPPING_START_EVENT: Mode::MAP_KEY" << std::endl;
 					mode.pop_back();