1

if i have xml content

<aaa> 1111<bbb>222</bbb>333 </aaa>

Then how to get value which is "1111" and "333"

but I only can get value of first node which is "1111" only

Please advise

code is here

#include <iostream>
#include "rapidxml/rapidxml.hpp"
#include <string.h>

using namespace std;
using namespace rapidxml;

string content = "<aaa> 1112<bbb>222</bbb>333 </aaa>";

int main(int argc, char**argv) {


    xml_document<> m_doc;

    try {

        m_doc.parse<0>(&content[0]);

    } catch (rapidxml::parse_error &error) {
        cout <<"parse_error\n";
        exit(1);

    }

     xml_node<>* node = m_doc.first_node();
     cout << "name = " <<node->name()<<endl;
     cout << "value = "<< node->value() <<endl;
     cout << "value_size = "<< node->value_size()<<endl;

    m_doc.clear();

    return 0;
}

Output is

name = aaa
value =  1111
value_size = 5
Denny
  • 449
  • 4
  • 17
  • never used that library before, but something tells me you should get what you want by cycling on the node list using `xml_node::next_sibling`(see tutorial at http://rapidxml.sourceforge.net/manual.html#classrapidxml_1_1xml__node). – didierc Oct 31 '14 at 06:49
  • @didierc using next_sibling , will get NULL point back , coz there is no sibling node – Denny Nov 03 '14 at 09:41

2 Answers2

0

Use next_sibling function to find the next string under same node. Same is used by Xerces library to find the next string under same node.

  • using next_sibling , will get NULL point back , coz there is no sibling node – Denny Nov 03 '14 at 09:42
  • DOMNode *domNodeP = GetDOMElement()->getFirstChild(); while(domNodeP != NULL && domNodeP->getNodeType() != DOMNode::ELEMENT_NODE) { domNodeP = domNodeP->getNextSibling(); } i used the above code for xerces to find the next sibling under any node like xyz lmn and this return me lmn. I think You should also try the same get the first child and then iterate through the node to find the next sibling(for text) – Ramesh Chander Nov 10 '14 at 07:01
0

Good question : I'd initially misread the XML structure you're trying to parse.

I haven't tried this, but I think that once you have found the first node <aaa> you must then get the first child node (which should have type node_data, value 1111 in your example.) , rather than getting the value of the node_element.

Then iterate over all it's siblings looking for other nodes with type node_data.

Roddy
  • 66,617
  • 42
  • 165
  • 277