From: Chai, J. (N-VB Enterprises) <jin...@lm...> - 2005-01-13 15:19:39
|
Hi, I am new to the libxml++(1.0) and trying to walk through the examples. For the dom_xpath example, I tried to do a dynamic_cast for the nodes contained in the NodeSet returned by the Node find method. But it always fails. Is there anything I am doing wrong? Thanks, Jin ======================= // -*- C++ -*- /* main.cc * * Copyright (C) 2002 The libxml++ development team * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <libxml++/libxml++.h> #include <iostream> void xpath_test(const xmlpp::Node* node, const std::string& xpath) { std::cout << std::endl; //Separate tests by an empty line. std::cout << "searching with xpath '" << xpath << "' in root node: " << std::endl; xmlpp::NodeSet set = node->find(xpath); std::cout << set.size() << " nodes have been found:" << std::endl; //Print the structural paths: for(xmlpp::NodeSet::iterator i = set.begin(); i != set.end(); ++i) { xmlpp::Element* eNode = dynamic_cast<xmlpp::Element*> (*i);//<==================== std::cout << " " << (*i)->get_path() << " " << (*i)->get_name()<<std::endl; if (eNode) std::cout << "Element name:" << eNode->get_name() << std::endl;// <======This line never get printed. } } int main(int argc, char* argv[]) { std::string filepath; if(argc > 1 ) filepath = argv[1]; //Allow the user to specify a different XML file to parse. else filepath = "example.xml"; try { xmlpp::DomParser parser(filepath); if(parser) { const xmlpp::Node* root = parser.get_document()->get_root_node(); //deleted by DomParser. if(root) { // Find all sections, no matter where: xpath_test(root, "//section"); // Find the title node (if there is one): xpath_test(root, "title"); std::cout << std::endl; // And finally test whether intra-document links are well-formed. // To be well-formed, the 'linkend' attribute must refer to // an element in terms of its 'id'. // // Find out whether there are linkend attributes that don't have // corresponding 'id's std::cout << "searching for unresolved internal references " << "(see docbook manual):" << std::endl; xpath_test(root, "//xref/@linkend"); } } } catch(const std::exception& ex) { std::cout << "Exception caught: " << ex.what() << std::endl; } return 0; } ======================== <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.0/docbookx.dtd"> <article> <title>An Article</title> <section id="section1"> <title>A section</title> <para>This is a paragraph.</para> </section> <section id="section2"> <title>Another section</title> <section id="section3"> <title>A sub-section</title> <para>This paragraph refers to <xref linkend="section1"/>, <xref linkend="section2"/>, and <xref linkend="section4"/>.</para> </section> </section> </article> |