xlnt/source/serialization/xml_node.cpp

140 lines
3.0 KiB
C++
Raw Normal View History

2015-11-02 01:31:29 +08:00
#include <xlnt/serialization/xml_node.hpp>
#include <xlnt/serialization/xml_serializer.hpp>
2015-10-31 06:54:04 +08:00
#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_node::xml_node() : d_(new detail::xml_node_impl)
2015-10-30 01:46:56 +08:00
{
}
2015-10-31 06:54:04 +08:00
xml_node::xml_node(const detail::xml_node_impl &d) : xml_node()
{
d_->node = d.node;
}
2015-10-31 06:54:04 +08:00
xml_node::~xml_node()
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
}
xml_node::xml_node(const xml_node &other) : xml_node()
{
d_->node = other.d_->node;
}
2015-10-31 06:54:04 +08:00
xml_node &xml_node::operator=(const xlnt::xml_node &other)
{
d_->node = other.d_->node;
2015-10-31 06:54:04 +08:00
return *this;
2015-10-30 01:46:56 +08:00
}
std::string xml_node::get_name() const
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
return d_->node.name();
2015-10-30 01:46:56 +08:00
}
void xml_node::set_name(const std::string &name)
2015-10-30 01:46:56 +08:00
{
d_->node.set_name(name.c_str());
2015-10-30 01:46:56 +08:00
}
bool xml_node::has_text() const
{
2015-10-31 06:54:04 +08:00
return d_->node.text() != nullptr;
2015-10-30 01:46:56 +08:00
}
std::string xml_node::get_text() const
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
return d_->node.text().as_string();
2015-10-30 01:46:56 +08:00
}
void xml_node::set_text(const std::string &text)
2015-10-30 01:46:56 +08:00
{
d_->node.text().set(text.c_str());
2015-10-30 01:46:56 +08:00
}
2015-10-31 06:54:04 +08:00
const std::vector<xml_node> xml_node::get_children() const
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
std::vector<xml_node> children;
for (auto child : d_->node.children())
2015-10-31 06:54:04 +08:00
{
children.push_back(xml_node(detail::xml_node_impl(child)));
}
2015-10-31 06:54:04 +08:00
return children;
2015-10-30 01:46:56 +08:00
}
2015-10-31 06:54:04 +08:00
xml_node xml_node::add_child(const xml_node &child)
2015-10-30 01:46:56 +08:00
{
auto child_node = xml_node(detail::xml_node_impl(d_->node.append_child(child.get_name().c_str())));
for (auto attr : child.get_attributes())
2015-10-31 06:54:04 +08:00
{
child_node.add_attribute(attr.first, attr.second);
}
for (auto child_child : child.get_children())
2015-10-31 06:54:04 +08:00
{
child_node.add_child(child_child);
}
2015-10-31 06:54:04 +08:00
return child_node;
2015-10-30 01:46:56 +08:00
}
xml_node xml_node::add_child(const std::string &child_name)
2015-10-30 01:46:56 +08:00
{
return xml_node(detail::xml_node_impl(d_->node.append_child(child_name.c_str())));
2015-10-30 01:46:56 +08:00
}
2015-10-31 06:54:04 +08:00
const std::vector<xml_node::string_pair> xml_node::get_attributes() const
2015-10-30 01:46:56 +08:00
{
2015-10-31 06:54:04 +08:00
std::vector<string_pair> attributes;
for (auto attr : d_->node.attributes())
2015-10-31 06:54:04 +08:00
{
attributes.push_back(std::make_pair<std::string, std::string>(attr.name(), attr.value()));
2015-10-31 06:54:04 +08:00
}
2015-10-31 06:54:04 +08:00
return attributes;
2015-10-30 01:46:56 +08:00
}
void xml_node::add_attribute(const std::string &name, const std::string &value)
2015-10-30 01:46:56 +08:00
{
d_->node.append_attribute(name.c_str()).set_value(value.c_str());
2015-10-30 01:46:56 +08:00
}
bool xml_node::has_attribute(const std::string &attribute_name) const
2015-10-30 01:46:56 +08:00
{
return d_->node.attribute(attribute_name.c_str()) != nullptr;
2015-10-30 01:46:56 +08:00
}
std::string xml_node::get_attribute(const std::string &attribute_name) const
2015-10-30 01:46:56 +08:00
{
return d_->node.attribute(attribute_name.c_str()).value();
2015-10-30 01:46:56 +08:00
}
bool xml_node::has_child(const std::string &child_name) const
2015-10-30 01:46:56 +08:00
{
return d_->node.child(child_name.c_str()) != nullptr;
2015-10-30 01:46:56 +08:00
}
xml_node xml_node::get_child(const std::string &child_name)
2015-10-30 07:37:07 +08:00
{
return xml_node(detail::xml_node_impl(d_->node.child(child_name.c_str())));
2015-10-30 07:37:07 +08:00
}
const xml_node xml_node::get_child(const std::string &child_name) const
2015-10-30 01:46:56 +08:00
{
return xml_node(detail::xml_node_impl(d_->node.child(child_name.c_str())));
2015-10-31 06:54:04 +08:00
}
std::string xml_node::to_string() const
2015-10-31 06:54:04 +08:00
{
return xml_serializer::serialize_node(*this);
2015-10-30 01:46:56 +08:00
}
} // namespace xlnt