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-11-01 22:43:01 +08:00
|
|
|
|
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
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
void xml_document::set_encoding(const 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
|
|
|
}
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
void xml_document::add_namespace(const string &id, const string &uri)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2015-11-05 07:45:03 +08:00
|
|
|
d_->doc.first_child().append_attribute((id.empty() ? "xmlns" : "xmlns:" + id).data()).set_value(uri.data());
|
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));
|
|
|
|
}
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
xml_node xml_document::add_child(const string &child_name)
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2015-11-05 07:45:03 +08:00
|
|
|
auto child = d_->doc.root().append_child(child_name.data());
|
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()));
|
|
|
|
}
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
string xml_document::to_string() const
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
|
|
|
return xml_serializer::serialize(*this);
|
|
|
|
}
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
xml_document &xml_document::from_string(const string &xml_string)
|
2015-10-31 06:54:04 +08:00
|
|
|
{
|
2015-11-05 07:45:03 +08:00
|
|
|
d_->doc.load(xml_string.data());
|
2015-11-01 22:43:01 +08:00
|
|
|
|
2015-10-31 06:54:04 +08:00
|
|
|
return *this;
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
xml_node xml_document::get_child(const string &child_name)
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2015-11-05 07:45:03 +08:00
|
|
|
return xml_node(detail::xml_node_impl(d_->doc.child(child_name.data())));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
const xml_node xml_document::get_child(const string &child_name) const
|
2015-10-30 01:46:56 +08:00
|
|
|
{
|
2015-11-05 07:45:03 +08:00
|
|
|
return xml_node(detail::xml_node_impl(d_->doc.child(child_name.data())));
|
2015-10-30 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2015-11-03 05:45:05 +08:00
|
|
|
} // namespace xlnt
|