xlnt/source/serialization/xml_document.cpp

106 lines
3.1 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-11-02 01:31:29 +08:00
#include <xlnt/serialization/xml_document.hpp>
#include <xlnt/serialization/xml_node.hpp>
#include <xlnt/serialization/xml_serializer.hpp>
2015-10-31 06:54:04 +08:00
2015-11-03 03:22:13 +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
namespace xlnt {
2015-10-31 06:54:04 +08:00
xml_document::xml_document() : d_(new detail::xml_document_impl())
{
}
xml_document::xml_document(const xml_document &other) : xml_document()
{
d_->doc.append_copy(other.d_->doc.root());
}
2015-11-03 03:22:13 +08:00
xml_document::xml_document(xml_document &&other)
{
std::swap(d_, other.d_);
}
2015-10-31 06:54:04 +08:00
xml_document::~xml_document()
{
}
2015-10-30 01:46:56 +08:00
void xml_document::set_encoding(const std::string &encoding)
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
d_->encoding = encoding;
2015-10-30 01:46:56 +08:00
}
void xml_document::add_namespace(const std::string &id, const std::string &uri)
2015-10-30 01:46:56 +08:00
{
d_->doc.first_child().append_attribute((id.empty() ? "xmlns" : "xmlns:" + id).c_str()).set_value(uri.c_str());
2015-10-31 06:54:04 +08:00
}
xml_node xml_document::add_child(const xml_node &child)
{
auto child_node = d_->doc.root().append_copy(child.d_->node);
return xml_node(detail::xml_node_impl(child_node));
}
xml_node xml_document::add_child(const std::string &child_name)
2015-10-31 06:54:04 +08:00
{
auto child = d_->doc.root().append_child(child_name.c_str());
2015-10-31 06:54:04 +08:00
return xml_node(detail::xml_node_impl(child));
}
xml_node xml_document::get_root()
{
return xml_node(detail::xml_node_impl(d_->doc.root()));
}
const xml_node xml_document::get_root() const
{
return xml_node(detail::xml_node_impl(d_->doc.root()));
}
std::string xml_document::to_string() const
2015-10-31 06:54:04 +08:00
{
return xml_serializer::serialize(*this);
}
xml_document &xml_document::from_string(const std::string &xml_string)
2015-10-31 06:54:04 +08:00
{
d_->doc.load(xml_string.c_str());
2015-10-31 06:54:04 +08:00
return *this;
2015-10-30 01:46:56 +08:00
}
xml_node xml_document::get_child(const std::string &child_name)
2015-10-30 01:46:56 +08:00
{
return xml_node(detail::xml_node_impl(d_->doc.child(child_name.c_str())));
2015-10-30 01:46:56 +08:00
}
const xml_node xml_document::get_child(const std::string &child_name) const
2015-10-30 01:46:56 +08:00
{
return xml_node(detail::xml_node_impl(d_->doc.child(child_name.c_str())));
2015-10-30 01:46:56 +08:00
}
2015-11-03 05:45:05 +08:00
} // namespace xlnt