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..
Parsing xml.. [message #35833] Tue, 27 March 2012 13:19 Go to next message
Monty.mvh is currently offline  Monty.mvh
Messages: 31
Registered: July 2011
Location: Bangalore
Member
Hi Friends,


I am trying to parse a xml file and i am referring to an example Xml.cpp present in reference section.

Say i have a xml content of below kind
  <Player name="john" score1="10" score2="20"  score3="30" score4="40" />
  <Player name="frank"  score1="40" score2="60"  score3="80" score4="20"/>



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

[Updated on: Tue, 27 March 2012 13:24]

Report message to a moderator

Re: Parsing xml.. [message #35843 is a reply to message #35833] Tue, 27 March 2012 21:16 Go to previous messageGo to next 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);
	}
}
Re: Parsing xml.. [message #35851 is a reply to message #35833] Wed, 28 March 2012 06:11 Go to previous message
Monty.mvh is currently offline  Monty.mvh
Messages: 31
Registered: July 2011
Location: Bangalore
Member
Thank you very much Sender Ghost.
This method was helpful .

[Updated on: Wed, 28 March 2012 13:17]

Report message to a moderator

Previous Topic: DialSlider
Next Topic: MySQL example compile issue
Goto Forum:
  


Current Time: Thu Jun 04 06:56:53 CEST 2020

Total time taken to generate the page: 0.00991 seconds