~ubuntu-branches/ubuntu/precise/libxml++2.6/precise

« back to all changes in this revision

Viewing changes to examples/dom_parser_raw/main.cc

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-03-13 15:46:33 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050313154633-iubyqdtu6y3p8915
Tags: 2.10.0-0ubuntu2
added doxygen to the build-depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- C++ -*-
 
2
 
 
3
/* main.cc
 
4
 *
 
5
 * Copyright (C) 2002 The libxml++ development team
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Library General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Library General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Library General Public
 
18
 * License along with this library; if not, write to the Free
 
19
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 */
 
21
 
 
22
#include <libxml++/libxml++.h>
 
23
 
 
24
#include <iostream>
 
25
#include <fstream>
 
26
#include <glibmm/convert.h>
 
27
 
 
28
 
 
29
void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
 
30
{
 
31
  std::cout << std::endl; //Separate nodes by an empty line.
 
32
  
 
33
  std::cout << "Node name = " << node->get_name() << std::endl;
 
34
 
 
35
  //Recurse through child nodes:
 
36
  xmlpp::Node::NodeList list = node->get_children();
 
37
  for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
 
38
  {
 
39
    print_node(*iter, indentation + 2); //recursive
 
40
  }
 
41
}
 
42
 
 
43
std::string read_from_disk(const std::string& filepath)
 
44
{
 
45
  std::string result;
 
46
 
 
47
  std::ifstream fStream(filepath.c_str());
 
48
  if(fStream.is_open())
 
49
  {
 
50
    while(!(fStream.eof()))
 
51
    {
 
52
      char chTemp = fStream.get();
 
53
      if(!(fStream.eof()))
 
54
        result += chTemp;
 
55
    }
 
56
  }
 
57
 
 
58
  return result;
 
59
}
 
60
 
 
61
int main(int argc, char* argv[])
 
62
{
 
63
  Glib::ustring filepath;
 
64
  if(argc > 1 )
 
65
    filepath = argv[1]; //Allow the user to specify a different XML file to parse.
 
66
  else
 
67
    filepath = "example.xml";
 
68
  
 
69
  try
 
70
  {
 
71
    xmlpp::DomParser parser;
 
72
    parser.set_validate();
 
73
    parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
 
74
   
 
75
 
 
76
    std::string contents = read_from_disk(filepath);
 
77
    std::string contents_ucs2;
 
78
 
 
79
    try
 
80
    {
 
81
      contents_ucs2 = Glib::convert(contents, "UCS-2", "UTF-8");
 
82
    }
 
83
    catch(const Glib::Error& ex)
 
84
    {
 
85
      std::cerr << "Glib::convert failed: " << ex.what() << std::endl;
 
86
    }
 
87
 
 
88
    parser.parse_memory_raw((const unsigned char*)contents_ucs2.c_str(), contents_ucs2.size());
 
89
 
 
90
    //Look at the first few bytes, to see whether it really looks like UCS2.
 
91
    //Because UCS2 uses 2 bytes, we would expect every second byte to be zero for our simple example:
 
92
    std::cout << "First 10 bytes of the UCS-2 data:" << std::endl;
 
93
    for(std::string::size_type i = 0; (i < 10) && (i < contents_ucs2.size()); ++i)
 
94
    {
 
95
      std::cout << std::hex << (int)contents_ucs2[i] << ", ";
 
96
    }
 
97
    std::cout << std::endl;
 
98
 
 
99
    if(parser)
 
100
    {
 
101
      //Walk the tree:
 
102
      const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser.
 
103
      print_node(pNode);
 
104
    }
 
105
  }
 
106
  catch(const std::exception& ex)
 
107
  {
 
108
    std::cout << "Exception caught: " << ex.what() << std::endl;
 
109
  }
 
110
 
 
111
  return 0;
 
112
}
 
113