xlnt/source/serialization/xml_serializer.cpp

63 lines
2.0 KiB
C++
Raw Normal View History

2015-12-25 06:10:02 +08:00
// Copyright (c) 2014-2016 Thomas Fussell
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE
//
// @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file
2015-10-30 01:46:56 +08:00
#include <sstream>
2015-11-02 01:31:29 +08:00
#include <xlnt/serialization/xml_serializer.hpp>
#include <xlnt/serialization/xml_document.hpp>
#include <xlnt/serialization/xml_node.hpp>
2015-10-30 01:46:56 +08:00
#include "detail/include_pugixml.hpp"
2015-10-31 06:54:04 +08:00
#include "detail/xml_document_impl.hpp"
#include "detail/xml_node_impl.hpp"
2015-10-30 01:46:56 +08:00
2015-10-31 06:54:04 +08:00
namespace xlnt {
2015-10-30 01:46:56 +08:00
std::string xml_serializer::serialize(const xml_document &xml)
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
std::ostringstream ss;
xml.d_->doc.save(ss, " ", pugi::format_default, pugi::encoding_utf8);
return ss.str();
2015-10-30 01:46:56 +08:00
}
xml_document xml_serializer::deserialize(const std::string &xml_string)
2015-10-31 06:54:04 +08:00
{
xml_document doc;
doc.d_->doc.load(xml_string.c_str());
2015-10-31 06:54:04 +08:00
return doc;
2015-10-30 01:46:56 +08:00
}
std::string xml_serializer::serialize_node(const xml_node &xml)
2015-10-30 01:46:56 +08:00
{
pugi::xml_document doc;
2015-10-31 06:54:04 +08:00
doc.append_copy(xml.d_->node);
2015-10-30 01:46:56 +08:00
std::ostringstream ss;
doc.save(ss);
return ss.str();
2015-10-30 01:46:56 +08:00
}
2015-10-30 01:46:56 +08:00
} // namespace xlnt