Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » [Solved] Recursively loop through XML node (Loop Through xml node)
icon8.gif  [Solved] Recursively loop through XML node [message #50411] Tue, 23 October 2018 10:46 Go to next message
Xemuth is currently offline  Xemuth
Messages: 22
Registered: August 2018
Location: France
Promising Member
Hello everyone !

today, I'm trying to loop through XmlNode to get all the text of it.
Here is my trying code :

#include <Core/Core.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace Upp;

Upp::String getRetour(const XmlNode &xn,int cpt){
	int i;
	XmlNode buffer(xn,cpt);
	if( buffer.GetCount() > 0 && buffer.IsTag()){
		Cout() << buffer.GetCount() << "\n";
		for( i = 0; i < buffer.GetCount(); i++){
			getRetour(buffer.Node(i),cpt++);	
		}	

	}
	else{
		Cout() << buffer.GetText();	
		return buffer.GetText();
	}	
}

CONSOLE_APP_MAIN
{
	String data;
	String atmTAg;
	std::ifstream fichier(String("myFile").ToStd());
	if(fichier)
	{
		std::string str((std::istreambuf_iterator<char>(fichier)),
	         			 std::istreambuf_iterator<char>());
		fichier.close();
		data = Upp::String(str);
	}
	XmlNode xn = ParseXML(data);
	getRetour(xn.Node(0),0);

}


This code crash when I reach the lowest part of my XML, it prints me the right text then crash after returning the string.

Have any idea of what cause the issue ? some tips about my Code ? Very Happy

Thanks in advance,

Have a good day

[Updated on: Tue, 23 October 2018 11:50]

Report message to a moderator

Re: Recursively loop through XML node [message #50413 is a reply to message #50411] Tue, 23 October 2018 11:49 Go to previous message
Xemuth is currently offline  Xemuth
Messages: 22
Registered: August 2018
Location: France
Promising Member
Hello again, I find au way to fix my issue and loop through XmlNode

void loopXml(const XmlNode &xn,int cpt,String baliseToFind,bool Ok){
	XmlNode buffer(xn,cpt);
		for(int i = 0; i < buffer.GetCount(); i++){
	if(buffer.GetText().Find(baliseToFind) > 0 || Ok ){
		//Cout() << buffer.GetText().Find(baliseToFind) <<"\n";
		if(buffer[i].IsText())
			Cout() << buffer[i].GetText() << "\n";
		else
			loopXml(buffer[i],cpt++,baliseToFind,true);
	}
	else
		loopXml(buffer[i],cpt++,baliseToFind,false);
	}
}

CONSOLE_APP_MAIN
{
	String data;
	String atmTAg;
	std::ifstream fichier(String("myFile").ToStd());
	if(fichier)
	{
		std::string str((std::istreambuf_iterator<char>(fichier)),
	         			 std::istreambuf_iterator<char>());
		fichier.close();
		data = Upp::String(str);
	}
	XmlNode xn = ParseXML(data);
	loopXml(xn,0,"breaks",false);
}


function loopXml show me every text in "breaks" tags
Previous Topic: Linking issue...cmd line vs U++ vs Codeblocks...
Next Topic: BOOST UDP issue
Goto Forum:
  


Current Time: Sat Feb 23 07:39:01 CET 2019

Total time taken to generate the page: 0.01062 seconds