5
* Copyright (C) 2002 The libxml++ development team
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.
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.
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.
22
#include <libxml++/libxml++.h>
26
#include <glibmm/convert.h>
29
void print_node(const xmlpp::Node* node, unsigned int indentation = 0)
31
std::cout << std::endl; //Separate nodes by an empty line.
33
std::cout << "Node name = " << node->get_name() << std::endl;
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)
39
print_node(*iter, indentation + 2); //recursive
43
std::string read_from_disk(const std::string& filepath)
47
std::ifstream fStream(filepath.c_str());
50
while(!(fStream.eof()))
52
char chTemp = fStream.get();
61
int main(int argc, char* argv[])
63
Glib::ustring filepath;
65
filepath = argv[1]; //Allow the user to specify a different XML file to parse.
67
filepath = "example.xml";
71
xmlpp::DomParser parser;
72
parser.set_validate();
73
parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
76
std::string contents = read_from_disk(filepath);
77
std::string contents_ucs2;
81
contents_ucs2 = Glib::convert(contents, "UCS-2", "UTF-8");
83
catch(const Glib::Error& ex)
85
std::cerr << "Glib::convert failed: " << ex.what() << std::endl;
88
parser.parse_memory_raw((const unsigned char*)contents_ucs2.c_str(), contents_ucs2.size());
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)
95
std::cout << std::hex << (int)contents_ucs2[i] << ", ";
97
std::cout << std::endl;
102
const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser.
106
catch(const std::exception& ex)
108
std::cout << "Exception caught: " << ex.what() << std::endl;