libxml++ r176 - in trunk: . examples/sax_parser



Author: murrayc
Date: Mon May  5 11:03:56 2008
New Revision: 176
URL: http://svn.gnome.org/viewvc/libxml++?rev=176&view=rev

Log:
2008-05-05  Murray Cumming  <murrayc murrayc com>

* examples/sax_parser/main.cc (main): Use parse_file() but leave 
the parse_chunk() version commented out, to simplify this example.
* examples/sax_parser/myparser.cc
Catch Glib::ConvertError exceptions when using std::cout, though 
libxml++ should really always supply valid UTF-8 to us.


Modified:
   trunk/ChangeLog
   trunk/examples/sax_parser/main.cc
   trunk/examples/sax_parser/myparser.cc

Modified: trunk/examples/sax_parser/main.cc
==============================================================================
--- trunk/examples/sax_parser/main.cc	(original)
+++ trunk/examples/sax_parser/main.cc	Mon May  5 11:03:56 2008
@@ -60,17 +60,28 @@
     
     std::ifstream is(filepath.c_str());
     char buffer[64];
+    const size_t buffer_size = sizeof(buffer) / sizeof(char);
 
+    //Parse the file:
     MySaxParser parser;
-    do {
-      is.read(buffer, 63);
-      Glib::ustring input(buffer, is.gcount());
+    parser.parse_file(filepath);
 
-      parser.parse_chunk(input);
+    //Or parse chunks (though this seems to have problems):
+/*
+    do
+    {
+      memset(buffer, 0, buffer_size);
+      is.read(buffer, buffer_size-1);
+      if(is && is.gcount())
+      {
+        Glib::ustring input(buffer, is.gcount());
+        parser.parse_chunk(input);
+      }
     }
     while(is);
 
     parser.finish_chunk_parsing();
+*/
   }
 
 

Modified: trunk/examples/sax_parser/myparser.cc
==============================================================================
--- trunk/examples/sax_parser/myparser.cc	(original)
+++ trunk/examples/sax_parser/myparser.cc	Mon May  5 11:03:56 2008
@@ -20,6 +20,7 @@
  */
 
 #include "myparser.h"
+#include <glibmm/convert.h> //For Glib::ConvertError
 
 #include <iostream>
 
@@ -50,7 +51,23 @@
   // Print attributes:
   for(xmlpp::SaxParser::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
   {
-    std::cout << "  Attribute " << iter->name << " = " << iter->value << std::endl;
+    try
+    {
+      std::cout << "  Attribute name=" << iter->name << std::endl;
+    }
+    catch(const Glib::ConvertError& ex)
+    {
+      std::cerr << "MySaxParser::on_start_element(): Exception caught while converting name for std::cout: " << ex.what() << std::endl;
+    }
+
+    try
+    {
+      std::cout << "    , value= " << iter->value << std::endl;
+    }
+    catch(const Glib::ConvertError& ex)
+    {
+      std::cerr << "MySaxParser::on_start_element(): Exception caught while converting value for std::cout: " << ex.what() << std::endl;
+    }
   }
 }
 
@@ -61,26 +78,61 @@
 
 void MySaxParser::on_characters(const Glib::ustring& text)
 {
-  std::cout << "on_characters(): " << text << std::endl;
+  try
+  {
+    std::cout << "on_characters(): " << text << std::endl;
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << "MySaxParser::on_characters(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
+  }
 }
 
 void MySaxParser::on_comment(const Glib::ustring& text)
 {
-  std::cout << "on_comment(): " << text << std::endl;
+  try
+  {
+    std::cout << "on_comment(): " << text << std::endl;
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << "MySaxParser::on_comment(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
+  }
 }
 
 void MySaxParser::on_warning(const Glib::ustring& text)
 {
-  std::cout << "on_warning(): " << text << std::endl;
+  try
+  {
+    std::cout << "on_warning(): " << text << std::endl;
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << "MySaxParser::on_warning(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
+  }
 }
 
 void MySaxParser::on_error(const Glib::ustring& text)
 {
-  std::cout << "on_error(): " << text << std::endl;
+  try
+  {
+    std::cout << "on_error(): " << text << std::endl;
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << "MySaxParser::on_error(): Exception caught while converting text for std::cout: " << ex.what() << std::endl;
+  }
 }
 
 void MySaxParser::on_fatal_error(const Glib::ustring& text)
 {
-  std::cout << "on_fatal_error(): " << text << std::endl;
+  try
+  {
+    std::cout << "on_fatal_error(): " << text << std::endl;
+  }
+  catch(const Glib::ConvertError& ex)
+  {
+    std::cerr << "MySaxParser::on_characters(): Exception caught while converting value for std::cout: " << ex.what() << std::endl;
+  }
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]