Add the ability to find the root node name of some XML.
authorCarl Hetherington <cth@carlh.net>
Tue, 8 Jul 2014 21:40:36 +0000 (22:40 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 8 Jul 2014 21:40:36 +0000 (22:40 +0100)
src/cxml.cc
src/cxml.h
test/tests.cc

index b251ae79610a45e4b27f3d31d19c8c0a566a210c..99055ee1da521aff6e0a4e12c9410f24eef22a3e 100644 (file)
@@ -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;
@@ -270,8 +275,10 @@ cxml::Document::take_root_node ()
        }
 
        _node = _parser->get_document()->get_root_node ();
-       if (_node->get_name() != _root_name) {
+       if (!_root_name.empty() && _node->get_name() != _root_name) {
                throw cxml::Error ("unrecognised root node");
+       } else if (_root_name.empty ()) {
+               _root_name = _node->get_name ();
        }
 }
 
index 0f57b924aa118df7b47f64c7877801db483cf8af..a8da82a42b8791a41e1eb1890f3e442b6b9314ad 100644 (file)
@@ -213,14 +213,19 @@ typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
 class Document : public Node
 {
 public:
+       Document ();
        Document (std::string root_name);
        Document (std::string root_name, boost::filesystem::path);
 
+       virtual ~Document ();
+
        void read_file (boost::filesystem::path);
        void read_stream (std::istream &);
        
-       virtual ~Document ();
-
+       std::string root_name () const {
+               return _root_name;
+       }
+              
 private:
        void take_root_node ();
        
index 77f13c63a8a8be5538f932ed4a2e1c15b31bdc86..9d07e7c19a7e6be7772327f344b4d317d6abb01c 100644 (file)
@@ -69,10 +69,13 @@ BOOST_AUTO_TEST_CASE (test)
 
        BOOST_CHECK (!document.optional_bool_child("G"));
 
-       BOOST_CHECK_EQUAL (document.node_children("H/I").size(), 2);
-       BOOST_CHECK_EQUAL (document.node_children("H/I").front()->content(), "testing");
-       BOOST_CHECK_EQUAL (document.node_children("H/I").back()->content(), "more testing");
+       list<shared_ptr<cxml::Node> > h = document.node_children ("H");
+       BOOST_CHECK_EQUAL (h.size(), 1);
+       BOOST_CHECK_EQUAL (h.front()->node_children("I").size(), 2);
+       BOOST_CHECK_EQUAL (h.front()->node_children("I").front()->content(), "testing");
+       BOOST_CHECK_EQUAL (h.front()->node_children("I").back()->content(), "more testing");
 
-       BOOST_CHECK_EQUAL (document.string_child("J/K"), "jim");
-       BOOST_CHECK (!document.optional_bool_child("G"));
+       BOOST_CHECK_EQUAL (document.node_children("J").size(), 1);
+       BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").size(), 1);
+       BOOST_CHECK_EQUAL (document.node_children("J").front()->node_children("K").front()->content(), "jim");
 }