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 » Parsing xml..
Re: Parsing xml.. [message #35843 is a reply to message #35833] Tue, 27 March 2012 21:16 Go to previous messageGo to previous message
Sender Ghost is currently offline  Sender Ghost
Messages: 301
Registered: November 2008
Senior Member
Hello, Monty.
Monty.mvh wrote on Tue, 27 March 2012 13:19

Please suggest a example or pseudocode to get these individual scores using XmlParser

#include <Core/Core.h>

using namespace Upp;

const String xml = "\
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n\
<Scores>\n\
	<Player name=\"john\" score1=\"10\" score2=\"20\"  score3=\"30\" score4=\"40\" />\n\
	<Player name=\"frank\"  score1=\"40\" score2=\"60\"  score3=\"80\" score4=\"20\"/>\n\
</Scores>\n";

CONSOLE_APP_MAIN
{
	Cout() << "Using XmlParser:\n";

	XmlParser p(xml);
	try {
		while (!p.IsTag())
			p.Skip();

		p.PassTag("Scores");
		while (!p.End())
			if (p.TagE("Player")) {
				String name = p["name"];
				int score1 = p.Int("score1"),
					score2 = p.Int("score2"),
					score3 = p.Int("score3"),
					score4 = p.Int("score4");

				Cout() << Format("%s: %d, %d, %d, %d\n", name, score1, score2, score3, score4);
			}
			else p.Skip();
	}
	catch(XmlError e) {
		Cerr() << "XmlError: " << e << '\n';
		SetExitCode(1);
		return;
	}

	Cout() << "\nUsing XmlNode:\n";

	XmlNode node;
	try {
		node = ParseXML(xml);
	}
	catch (XmlError e) {
		Cerr() << "XmlError: " << e << '\n';
		SetExitCode(1);
		return;
	}

	const XmlNode& lscores = node["Scores"];
	if (lscores.IsVoid()) {
		Cerr() << "There is no 'Scores' tag";
		SetExitCode(1);
		return;
	}

	for (int i = 0, n = lscores.GetCount(); i < n; ++i) {
		const XmlNode& lplayer = lscores[i];
		if (!lplayer.IsTag("Player"))
			continue;

		String name = lplayer.Attr("name");
		int score1 = ScanInt(lplayer.Attr("score1")),
			score2 = ScanInt(lplayer.Attr("score2")),
			score3 = ScanInt(lplayer.Attr("score3")),
			score4 = ScanInt(lplayer.Attr("score4"));

		Cout() << Format("%s: %d, %d, %d, %d\n", name, score1, score2, score3, score4);
	}
}
 
Read Message
Read Message
Read Message
Previous Topic: DialSlider
Next Topic: MySQL example compile issue
Goto Forum:
  


Current Time: Mon May 06 23:27:10 CEST 2024

Total time taken to generate the page: 0.02723 seconds