Changeset 6:93cf8f073247 in rrlib_xml


Ignore:
Timestamp:
30.08.2010 16:04:23 (9 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Phase:
public
Convert:
svn:3219ad6e-c0b7-4ac2-9554-e22e195eef7a/trunk@7
Message:
  • Added creation and extension of DOM trees
  • Added test program
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • make.xml

    r1 r6  
    66    </sources> 
    77  </rrlib> 
     8   
     9  <testprogram name="program" libs="rrlib_xml2_wrapper"> 
     10    <sources> 
     11      test/*.cpp 
     12    </sources> 
     13  </testprogram> 
    814 
    915</targets> 
  • tXMLDocument.cpp

    r4 r6  
    3333// External includes (system with <>, local with "") 
    3434//---------------------------------------------------------------------- 
    35 #include <cassert> 
     35#include <iostream> 
    3636 
    3737//---------------------------------------------------------------------- 
     
    4343// Debugging 
    4444//---------------------------------------------------------------------- 
     45#include <cassert> 
    4546 
    4647//---------------------------------------------------------------------- 
     
    6465// tXMLDocument constructors 
    6566//---------------------------------------------------------------------- 
     67tXMLDocument::tXMLDocument() 
     68    : document(xmlNewDoc(reinterpret_cast<const xmlChar *>("1.0"))), 
     69    root_node(0) 
     70{ 
     71  assert(this->document); 
     72} 
     73 
    6674tXMLDocument::tXMLDocument(const std::string &file_name, bool validate) 
    6775    : document(xmlReadFile(file_name.c_str(), 0, validate ? XML_PARSE_DTDVALID : 0)), 
     
    8795// tXMLDocument GetRootNode 
    8896//---------------------------------------------------------------------- 
    89 const tXMLNode &tXMLDocument::GetRootNode() const 
     97tXMLNode &tXMLDocument::GetRootNode() const 
    9098{ 
    9199  if (!this->root_node) 
    92100  { 
     101    xmlNodePtr node = xmlDocGetRootElement(this->document); 
     102    if (!node) 
     103    { 
     104      throw tXML2WrapperException("No root node defined for this document!"); 
     105    } 
    93106    this->root_node = new tXMLNode(xmlDocGetRootElement(this->document)); 
    94107  } 
    95108  return *this->root_node; 
    96109} 
     110 
     111//---------------------------------------------------------------------- 
     112// tXMLDocument AddRootNode 
     113//---------------------------------------------------------------------- 
     114tXMLNode &tXMLDocument::AddRootNode(const std::string &name) 
     115{ 
     116  if (this->root_node) 
     117  { 
     118    throw tXML2WrapperException("Root node already exists with name `" + name + "'!"); 
     119  } 
     120  this->root_node = new tXMLNode(xmlNewNode(0, reinterpret_cast<const xmlChar *>(name.c_str()))); 
     121  xmlDocSetRootElement(this->document, this->root_node->node); 
     122  return *this->root_node; 
     123} 
     124 
     125//---------------------------------------------------------------------- 
     126// tXMLDocument WriteToFile 
     127//---------------------------------------------------------------------- 
     128void tXMLDocument::WriteToFile(const std::string &file_name, int compression) const 
     129{ 
     130  if (compression) 
     131  { 
     132    xmlSetDocCompressMode(this->document, compression); 
     133  } 
     134  xmlSaveFormatFileEnc(file_name.c_str(), this->document, "UTF-8", 1); 
     135} 
  • tXMLDocument.h

    r5 r6  
    9292public: 
    9393 
    94   /*! The ctor of tXMLDocument 
     94  /*! The ctor of an empty tXMLDocument 
     95   * 
     96   * This ctor creates a new xml document 
     97   */ 
     98  tXMLDocument(); 
     99 
     100  /*! The ctor of tXMLDocument from a given file 
    95101   * 
    96102   * This ctor reads and parses a file with given name into a XML DOM 
     
    104110   * \exception tXML2WrapperException is thrown if the file was not found or could not be parsed 
    105111   */ 
    106   tXMLDocument(const std::string &file_name, bool validate = true); 
     112  explicit tXMLDocument(const std::string &file_name, bool validate = true); 
    107113 
    108114  /*! The dtor of tXMLDocument 
     
    117123   * \returns A reference to the root node 
    118124   */ 
    119   const tXMLNode &GetRootNode() const; 
     125  tXMLNode &GetRootNode() const; 
     126 
     127  tXMLNode &AddRootNode(const std::string &name); 
     128 
     129  void WriteToFile(const std::string &file_name, int compression = 0) const; 
    120130 
    121131}; 
  • tXMLNode.cpp

    r4 r6  
    6868    children(0) 
    6969{ 
     70  assert(node); 
    7071  assert(node->type == XML_ELEMENT_NODE); 
    7172  if (node->type != XML_ELEMENT_NODE) 
     
    113114  return *this->name; 
    114115} 
     116 
     117//---------------------------------------------------------------------- 
     118// tXMLNode AddChildNode 
     119//---------------------------------------------------------------------- 
     120tXMLNode &tXMLNode::AddChildNode(const std::string &name) 
     121{ 
     122  xmlNodePtr child_node = xmlNewChild(this->node, 0, reinterpret_cast<const xmlChar*>(name.c_str()), 0); 
     123  this->GetChildren(); 
     124  this->children->push_back(tXMLNode(child_node)); 
     125  return this->children->back(); 
     126} 
  • tXMLNode.h

    r5 r6  
    5050 
    5151#include <string> 
     52#include <sstream> 
    5253#include <vector> 
    5354#include <algorithm> 
     
    101102    char *endptr; 
    102103    TNumber result = convert_function(value.c_str(), &endptr, base); 
    103     if (errno || endptr) 
     104    if (errno || *endptr) 
    104105    { 
     106      printf("errno = %d\n*endptr = '%c'\n", errno, *endptr); 
     107      printf("ERANGE = %d\n", ERANGE); 
    105108      throw tXML2WrapperException("Could not convert `" + value + "' to number!"); 
    106109    } 
     
    114117    char *endptr; 
    115118    TNumber result = convert_function(value.c_str(), &endptr); 
    116     if (errno || endptr) 
     119    if (errno || *endptr) 
    117120    { 
     121      printf("errno = %d\n*endptr = '%c'\n", errno, *endptr); 
     122      printf("ERANGE = %d\n", ERANGE); 
    118123      throw tXML2WrapperException("Could not convert `" + value + "' to number!"); 
    119124    } 
     
    131136   */ 
    132137  tXMLNode(xmlNodePtr node); 
     138 
     139  inline void AddStringAttribute(const std::string &name, const std::string &value) 
     140  { 
     141    xmlNewProp(this->node, reinterpret_cast<const xmlChar *>(name.c_str()), reinterpret_cast<const xmlChar *>(value.c_str())); 
     142  } 
    133143 
    134144//---------------------------------------------------------------------- 
     
    327337  } 
    328338 
     339  tXMLNode &AddChildNode(const std::string &name); 
     340 
     341  template <typename TValue> 
     342  inline void AddAttribute(const std::string &name, const TValue &value) 
     343  { 
     344    std::stringstream converted_value; 
     345    converted_value << value; 
     346    this->AddStringAttribute(name, converted_value.str()); 
     347  } 
     348 
    329349}; 
    330350 
Note: See TracChangeset for help on using the changeset viewer.