Changeset 63:6e56f95badd3 in rrlib_xml


Ignore:
Timestamp:
25.02.2016 02:17:19 (4 years ago)
Author:
Jens Wettach <wettach@…>
Branch:
default
Phase:
public
Message:

added a function for searching a node in an XML document, added corresponding test cases

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tDocument.cpp

    r57 r63  
    3535#include <iostream> 
    3636 
     37extern "C" 
     38{ 
     39#include <libxml/xpath.h> 
     40} 
     41 
    3742//---------------------------------------------------------------------- 
    3843// Internal includes with "" 
     
    174179 
    175180//---------------------------------------------------------------------- 
     181// tDocument FindNode 
     182//---------------------------------------------------------------------- 
     183const tNode &tDocument::FindNode(const std::string &name) const 
     184{ 
     185  xmlXPathContext *xpath_context = xmlXPathNewContext(this->document); // requires cleanup 
     186  if (xpath_context == nullptr) 
     187  { 
     188    throw tException("Could not create the XPath context!"); 
     189  } 
     190 
     191  xmlXPathObject *xpath_object = xmlXPathEvalExpression((xmlChar*)name.c_str(), xpath_context); // requires cleanup 
     192  if (xpath_object == nullptr) 
     193  { 
     194    xmlXPathFreeContext(xpath_context); // clean up the XPath context 
     195    throw tException("Could not evaluate the XPath expression!"); 
     196  } 
     197 
     198  if ((xpath_object->nodesetval == nullptr) || (xpath_object->nodesetval->nodeTab == nullptr)) 
     199  { 
     200    xmlXPathFreeObject(xpath_object); // clean up the XPath object 
     201    xmlXPathFreeContext(xpath_context); // clean up the XPath context 
     202    throw tException("Could not find the expected node!"); 
     203  } 
     204  xmlNode *node = xpath_object->nodesetval->nodeTab[0]; // fetch node from query result 
     205  xmlXPathFreeObject(xpath_object); // clean up the XPath object 
     206  xmlXPathFreeContext(xpath_context); // clean up the XPath context 
     207  return *reinterpret_cast<tNode *>(node); 
     208} 
     209 
     210//---------------------------------------------------------------------- 
    176211// tDocument WriteToFile 
    177212//---------------------------------------------------------------------- 
  • tDocument.h

    r57 r63  
    197197  tNode &AddRootNode(const std::string &name); 
    198198 
     199 
     200  /*! Find a node in this XML document via a path in DOM tree representation 
     201   * 
     202   * Given a path to the searched node, an xpath query is executed 
     203   * on the DOM tree representation of this document in order to locate 
     204   * the respective XML node. If the query is successful, a reference 
     205   * to the node is returned, otherwise an exception is thrown. 
     206   * 
     207   * \exception tException is thrown if the query cannot be executed or the node cannot be found 
     208   * 
     209   * \param name   The path to the searched node in the DOM tree of this document 
     210   * 
     211   * \returns A reference to the found node 
     212   */ 
     213  const tNode &FindNode(const std::string &name) const; 
     214 
     215 
    199216  /*! Write the XML document to a file 
    200217   * 
  • tests/test.cpp

    r61 r63  
    163163    RRLIB_UNIT_TESTS_EQUALITY(std::string("text1text2text3"), doc.RootNode().FirstChild().GetTextContent()); 
    164164 
     165    RRLIB_UNIT_TESTS_EQUALITY(std::string("<child2>text2</child2>"), doc.FindNode("/foo/child1/child2").GetXMLDump()); 
     166 
    165167    doc.RootNode().FirstChild().RemoveTextContent(); 
    166168 
     
    172174 
    173175    RRLIB_UNIT_TESTS_EQUALITY(std::string("<test prop_1=\"val\" prop_2=\"true\" prop_3=\"4.3\" prop_4=\"123\"><test1 prop_1=\"val_1\" prop_2=\"true\" prop_3=\"4.3\"/><child1><child2>text2</child2></child1></test>"), this->document.RootNode().GetXMLDump()); 
     176 
     177    RRLIB_UNIT_TESTS_EQUALITY(std::string("<test1 prop_1=\"val_1\" prop_2=\"true\" prop_3=\"4.3\"/>"), this->document.FindNode("/test/test1").GetXMLDump()); 
     178 
     179    RRLIB_UNIT_TESTS_EXCEPTION(this->document.FindNode("/test/test2"), tException); 
    174180  } 
    175181}; 
Note: See TracChangeset for help on using the changeset viewer.