LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2004, 03:16 PM   #1
SeriousMan
LQ Newbie
 
Registered: Sep 2004
Distribution: Slackware
Posts: 24

Rep: Reputation: 15
Retrieving element content in libxml++-1.0


How can i retrieve element content in libxml++-1.0? The examples don't show me how to do that, and there's no additional documentation on it
 
Old 11-21-2004, 07:42 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
By using the get_content method.
 
Old 11-22-2004, 10:34 AM   #3
SeriousMan
LQ Newbie
 
Registered: Sep 2004
Distribution: Slackware
Posts: 24

Original Poster
Rep: Reputation: 15
Ok, but how can i get an instance of ContentNode class? I see no way to do this, and when trying to perform dynamic_cast, an exception occurs, claiming that nodes of this type don't have a content.
 
Old 11-22-2004, 11:44 AM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
A sample:
Code:
#include <libxml++/libxml++.h>
#include <iostream>
using namespace std;

void spaces(int n) {
  for(int i=0;i<n;i++) cout << " ";
}

void parseElement(xmlpp::Element *e,int depth) {
  
  spaces(depth-1);
  cout << ">>" << e->get_name() << endl;
  xmlpp::Element::NodeList children = e->get_children();
  for(xmlpp::Element::NodeList::iterator i = children.begin();i != children.end();i++) {
    spaces(depth);
    cout <<  "-" << (*i)->get_name() << endl;
    xmlpp::Element *foo=dynamic_cast<xmlpp::Element*>(*i);
    if(foo) parseElement(foo,depth+1);
    xmlpp::ContentNode *con=dynamic_cast<xmlpp::ContentNode*>(*i);
    if(con) {
      spaces(depth);
      cout << " A content: " << con->get_name() << endl;
      spaces(depth);
      cout << " Contents: \"" << con->get_content() << "\"\n";
    }
  }
  spaces(depth-1);
  cout << "<<" << e->get_name() << endl;

}

int main() 
 {
    xmlpp::DomParser* test=new xmlpp::DomParser("atest.xml");
    xmlpp::Document* test_xml = test->get_document();
    xmlpp::Element*  root_node = test_xml->get_root_node();
    cout << "Found root node named: " << root_node->get_name() << endl;
    parseElement(root_node,1);
    delete test;
  }
Compile that with
Code:
g++ `pkg-config --cflags libxml++-2.6` xmlpp3.cpp -o xmlpp `pkg-config --libs libxml++-2.6`
A sample input, atest.xml:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<aDocument>
<anItem>
  <entry>Lallalaa.. </entry>
  <entry>Foobar</entry>
  <rec><rec2>Bazlf</rec2></rec>
  <attrEntry data="foo"/>
</anItem>
</aDocument>
Output:
Code:
Found root node named: aDocument
>>aDocument
 -text
  A content: text
  Contents: "
"
 -anItem
 >>anItem
  -text
   A content: text
   Contents: "
  "
  -entry
  >>entry
   -text
    A content: text
    Contents: "Lallalaa.. "
  <<entry
  -text
   A content: text
   Contents: "
  "
  -entry
  >>entry
   -text
    A content: text
    Contents: "Foobar"
  <<entry
  -text
   A content: text
   Contents: "
  "
  -rec
  >>rec
   -rec2
   >>rec2
    -text
     A content: text
     Contents: "Bazlf"
   <<rec2
  <<rec
  -text
   A content: text
   Contents: "
  "
  -attrEntry
  >>attrEntry
  <<attrEntry
  -text
   A content: text
   Contents: "
"
 <<anItem
 -text
  A content: text
  Contents: "
"
<<aDocument

Last edited by ToniT; 11-22-2004 at 11:45 AM.
 
Old 11-22-2004, 12:14 PM   #5
SeriousMan
LQ Newbie
 
Registered: Sep 2004
Distribution: Slackware
Posts: 24

Original Poster
Rep: Reputation: 15
Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
libglade and libxml problem bambolin Linux - General 2 03-20-2005 01:35 PM
Problems with GTK and libXML when compiling HELP! britishnemesis Linux - Software 2 02-18-2005 01:27 PM
libXML problem?? Ytsejammer Linux - Software 0 12-23-2004 11:01 PM
Apache serving content after the content is removed? jrbush82 Linux - Software 6 05-05-2004 04:39 AM
problem installing XML::LibXML pmedes Linux - Newbie 0 10-28-2003 10:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration