Kitlist
A list manager for maintaining kit lists
Loading...
Searching...
No Matches
xml_writer.cpp
Go to the documentation of this file.
1/*
2
3 This file is part of Kitlist, a program to maintain a simple list
4 of items and assign items to one or more categories.
5
6 Copyright (C) 2008-2025 Frank Dean
7
8 Kitlist is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Kitlist is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Kitlist. If not, see <http://www.gnu.org/licenses/>.
20
21*/
22#include "xml_writer.hpp"
23#include "model.hpp"
24#include "pugixml.hpp"
25
26using namespace pugi;
27using namespace std;
28
29bool XmlWriter::save_file(const string& filename, const Model& model)
30{
31 xml_document doc;
32 xml_node decl = doc.prepend_child(node_declaration);
33 decl.append_attribute("version") = "1.0";
34 decl.append_attribute("encoding") = "UTF-8";
35 xml_node kitlist_node = doc.append_child("kitlist");
36 xml_node items_node = kitlist_node.append_child("items");
37 for (const auto& item : model.get_all_items()) {
38 xml_node item_node = items_node.append_child("item");
39 item_node.text() = item->get_name().c_str();
40 item_node.append_attribute("id").set_value(item->get_id());
41 item_node.append_attribute("checked").set_value(item->is_checked());
42 // item_node.append_child(node_pcdata).set_value(item->name);
43 }
44
45 xml_node categories_node = kitlist_node.append_child("categories");
46 for (const auto& cat : model.get_categories()) {
47 xml_node cat_node = categories_node.append_child("category");
48 cat_node.append_attribute("id").set_value(cat->id);
49 xml_node cat_name = cat_node.append_child("category-name");
50 cat_name.text() = cat->name.c_str();
51 xml_node cat_items_node = cat_node.append_child("category-items");
52 for (const auto& ci : cat->items) {
53 xml_node ci_node = cat_items_node.append_child("category-item");
54 ci_node.append_attribute("id").set_value(ci->get_id());
55 }
56 }
57 // cout << "Saving to " << filename << endl;
58 return doc.save_file(filename.c_str(), " ");
59}
Maintains the data model.
Definition model.hpp:40
const std::vector< std::shared_ptr< Category > > get_categories() const
The list of categories.
Definition model.hpp:527
const std::vector< std::shared_ptr< Item > > get_all_items() const
Gets all items, without filtering.
Definition model.hpp:537
static bool save_file(const std::string &filename, const Model &model)
Saves the Model in XML format to the specified file.
Copyright 2008-2025 Frank Dean