X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fcxml.cc;h=a9ab7697a97c0cac7c29cd6160e5c3ac19aa354b;hb=b134f1d3a23136aaaaa06f1785bac0d7b894c775;hp=b251ae79610a45e4b27f3d31d19c8c0a566a210c;hpb=df3b86a07de00a1a48845bc82e2d96aa0fc531dc;p=libcxml.git diff --git a/src/cxml.cc b/src/cxml.cc index b251ae7..a9ab769 100644 --- a/src/cxml.cc +++ b/src/cxml.cc @@ -142,12 +142,12 @@ cxml::Node::string_attribute (string name) const { xmlpp::Element const * e = dynamic_cast (_node); if (!e) { - throw cxml::Error ("missing attribute"); + throw cxml::Error ("missing attribute " + name); } xmlpp::Attribute* a = e->get_attribute (name); if (!a) { - throw cxml::Error ("missing attribute"); + throw cxml::Error ("missing attribute " + name); } return a->get_value (); @@ -239,6 +239,11 @@ cxml::Document::Document (string root_name, boost::filesystem::path file) read_file (file); } +cxml::Document::Document () +{ + _parser = new xmlpp::DomParser (); +} + cxml::Document::~Document () { delete _parser; @@ -248,7 +253,7 @@ void cxml::Document::read_file (filesystem::path file) { if (!filesystem::exists (file)) { - throw cxml::Error ("XML file does not exist"); + throw cxml::Error ("XML file " + file.string() + " does not exist"); } _parser->parse_file (file.string ()); @@ -262,6 +267,14 @@ cxml::Document::read_stream (istream& stream) take_root_node (); } +void +cxml::Document::read_string (string s) +{ + stringstream t (s); + _parser->parse_stream (t); + take_root_node (); +} + void cxml::Document::take_root_node () { @@ -270,8 +283,10 @@ cxml::Document::take_root_node () } _node = _parser->get_document()->get_root_node (); - if (_node->get_name() != _root_name) { - throw cxml::Error ("unrecognised root node"); + if (!_root_name.empty() && _node->get_name() != _root_name) { + throw cxml::Error ("unrecognised root node " + _node->get_name() + " (expecting " + _root_name + ")"); + } else if (_root_name.empty ()) { + _root_name = _node->get_name (); } }