Home » U++ Library support » U++ Core » how to use xmlparser to parse this document?
Re: how to use xmlparser to parse this document? [message #49124 is a reply to message #49120] |
Wed, 20 December 2017 15:12  |
Oblivion
Messages: 1212 Registered: August 2007
|
Senior 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
Github page: https://github.com/ismail-yilmaz
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Wed, 20 December 2017 20:28] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Mon Jun 16 21:57:13 CEST 2025
Total time taken to generate the page: 0.04636 seconds
|