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 » U++ Library support » U++ Core » how to use xmlparser to parse this document?
how to use xmlparser to parse this document? [message #49120] Wed, 20 December 2017 04:00 Go to next message
akebee is currently offline  akebee
Messages: 88
Registered: August 2011
Location: China
Member
i still do not quite understand the use of XmlParser Embarassed
such as parse this xml document:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>frames</key>
        <dict>
            <key>Comm1TipBg.png</key>
            <dict>
                <key>frame</key>
                <string>{{314,805},{120,120}}</string>
                <key>offset</key>
                <string>{0,0}</string>
                <key>rotated</key>
                <false/>
                <key>sourceColorRect</key>
                <string>{{0,0},{120,120}}</string>
                <key>sourceSize</key>
                <string>{120,120}</string>
            </dict>
            <key>Common1Back.png</key>
            <dict>
                <key>frame</key>
                <string>{{791,462},{62,54}}</string>
                <key>offset</key>
                <string>{0,0}</string>
                <key>rotated</key>
                <true/>
                <key>sourceColorRect</key>
                <string>{{0,0},{62,54}}</string>
                <key>sourceSize</key>
                <string>{62,54}</string>
            </dict>
            <key>Common1Button1.png</key>
            <dict>
                <key>frame</key>
                <string>{{766,257},{132,64}}</string>
                <key>offset</key>
                <string>{0,0}</string>
                <key>rotated</key>
                <false/>
                <key>sourceColorRect</key>
                <string>{{0,0},{132,64}}</string>
                <key>sourceSize</key>
                <string>{132,64}</string>
            </dict>
            <key>Common1Button1Disable.png</key>
            <dict>
                <key>frame</key>
                <string>{{314,927},{132,64}}</string>
                <key>offset</key>
                <string>{0,0}</string>
                <key>rotated</key>
                <false/>
                <key>sourceColorRect</key>
                <string>{{0,0},{132,64}}</string>
                <key>sourceSize</key>
                <string>{132,64}</string>
            </dict>
            <key>Common1Button2.png</key>
            <dict>
                <key>frame</key>
                <string>{{149,955},{132,64}}</string>
                <key>offset</key>
                <string>{0,0}</string>
                <key>rotated</key>
                <false/>
                <key>sourceColorRect</key>
                <string>{{0,0},{132,64}}</string>
                <key>sourceSize</key>
                <string>{132,64}</string>
            </dict>
            <key>Common1Button3.png</key>
            <dict>
                <key>frame</key>
                <string>{{559,514},{109,52}}</string>
                <key>offset</key>
                <string>{0,0}</string>
                <key>rotated</key>
                <true/>
                <key>sourceColorRect</key>
                <string>{{0,0},{109,52}}</string>
                <key>sourceSize</key>
                <string>{109,52}</string>
            </dict>
            <key>CommonVipSmall09.png</key>
            <dict>
                <key>frame</key>
                <string>{{937,823},{58,22}}</string>
                <key>offset</key>
                <string>{0,0}</string>
                <key>rotated</key>
                <false/>
                <key>sourceColorRect</key>
                <string>{{0,0},{58,22}}</string>
                <key>sourceSize</key>
                <string>{58,22}</string>
            </dict>
            <key>CommonVipSmall10.png</key>
            <dict>
                <key>frame</key>
                <string>{{917,799},{58,22}}</string>
                <key>offset</key>
                <string>{0,0}</string>
                <key>rotated</key>
                <false/>
                <key>sourceColorRect</key>
                <string>{{0,0},{58,22}}</string>
                <key>sourceSize</key>
                <string>{58,22}</string>
            </dict>
        </dict>
        <key>metadata</key>
        <dict>
            <key>format</key>
            <integer>2</integer>
            <key>realTextureFileName</key>
            <string>Common1.png</string>
            <key>size</key>
            <string>{1024,1024}</string>
            <key>smartupdate</key>
            <string>$TexturePacker:SmartUpdate:dc2b5d576da62f910d03eadb09babf4b$</string>
            <key>textureFileName</key>
            <string>Common1.png</string>
        </dict>
    </dict>
</plist>


my code Crying or Very Sad
	XmlParser pser(plist);
	pser.Relaxed();
	try{
		while(!pser.IsTag())
			pser.Skip();
		pser.PassTag("plist");
		pser.PassTag("dict");
		while(!pser.IsEof())
			if(pser.Tag("key"))
			{
				String key = pser.ReadText();
			//	DUMP(key);
				pser.PassEnd();
			}
			else if(pser.TagElseSkip("dict"))
			{
				while(!pser.IsEof())
				{
					if(pser.Tag("key"))
					{
						String key = pser.ReadText();
						DUMP(key);
						pser.PassEnd();
					}	
					else if(pser.Tag("dict"))
					{
					}
					else
						pser.Skip();			
				}
				pser.PassEnd();
			}
			
	}
	catch(XmlError e){
		LOG("ERROR: " << e);
	}
Crying or Very Sad Crying or Very Sad
Re: how to use xmlparser to parse this document? [message #49124 is a reply to message #49120] Wed, 20 December 2017 15:12 Go to previous message
Oblivion is currently offline  Oblivion
Messages: 574
Registered: August 2007
Location: Turkey
Contributor
Hello akebee.

	try{
		while(!pser.IsTag())
			pser.Skip();
		pser.PassTag("plist");
		pser.PassTag("dict");
		while(!pser.IsEof())
			if(pser.Tag("key"))
			{
				String key = pser.ReadText();
				DUMP(key);
				pser.PassEnd();
			}
			else
			if(pser.Tag("dict")) {
				pser.Skip();
			//	continue;
			}
			else
				pser.Skip();
			
	}
	catch(XmlError e){
		LOG("ERROR: " << e);
	}



Hovever, if you are not trying to write a better (faster) XML parser than UPP's parser, or not exercising your parsing skills I suggest you using ParseXML with XMLNode.
For the most cases I found them to be a better solution.


#include <Core/Core.h>

using namespace Upp;

void GetKeys(const XmlNode& node)
{
        // This is simply to give you the general idea. It is not optimized.

	if(node.GetTag() == "key")
		LOG(node.Node(0).GetText());
        else
 	   for(auto& subnode : node)
		 GetKeys(subnode);                   // Unless the node structure is "extremely deep", recursion is fine.
}

CONSOLE_APP_MAIN
{
	StdLogSetup(LOG_COUT | LOG_FILE);
	FileIn fin(GetDataFile("plist.xml"));
	XmlParser parser(fin);
	parser.Relaxed();
	try {
		auto xml = ParseXML(parser);
		GetKeys(xml);
	}
	catch(XmlError& e) {                              // <-- As a side note: try not to catch any exceptions "by value", catch them "by reference"
		LOG("Xml Parser error: " << e);
	}
}




Best regards,
Oblivion


[Updated on: Wed, 20 December 2017 20:28]

Report message to a moderator

Previous Topic: Fixes to Array::Create & Vector::Create
Next Topic: ParseJSON now throws exception
Goto Forum:
  


Current Time: Fri Aug 23 07:02:26 CEST 2019

Total time taken to generate the page: 0.01246 seconds