Home » Community » Newbie corner » Parsing xml.. 
	
		
		
			| Parsing xml.. [message #35833] | 
			Tue, 27 March 2012 13:19   | 
		 
		
			
				
				
				
					
						  
						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    | 
		 
		
			
				
				
				
					
						  
						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);
	}
}
 
		
		
		
 |  
	| 
		
	 | 
 
 
 |  
	| 
		
 |   
Goto Forum:
 
 Current Time: Tue Nov 04 11:02:49 CET 2025 
 Total time taken to generate the page: 0.04569 seconds 
 |