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 » problem when parse html with XMLParser
Re: problem when parse html with XMLParser [message #48806 is a reply to message #48805] Sat, 23 September 2017 09:35 Go to previous messageGo to previous message
mirek is currently offline  mirek
Messages: 14258
Registered: November 2005
Ultimate Member
First of all, it is not a crash, but exception:

CONSOLE_APP_MAIN
{
	String xml =
		"<html>"
		"<head><meta content=\"always\" name=\"referrer\"></head>"
		"<TITLE>A Midsummer Night's Dream</TITLE>"
		"</html>";
		
	XmlParser p(xml);
	try {
		while(!p.IsTag())
			p.Skip();
		p.PassTag("html");
		while(!p.End())
			if(p.Tag("head"))
			{
				while(!p.End()) {
					if(p.TagE("meta")) {
						// *exception* here because "<meta" dones`t have "/>"
					}
					else
						p.Skip();
				}
			}
		
			if(p.Tag("TITLE")) {
				String TITLE = p.ReadText();
				LOG(TITLE);
				p.PassEnd();
			}
			else
				p.Skip();
	}
	catch(XmlError) {
		LOG("ERROR!");
	}
}



...that covers case when you need to detect invalid XML, but not need to parse it.

U++ can even parse invalid XML, using Relaxed or Raw flags:


CONSOLE_APP_MAIN
{
	String xml =
		"<html>"
		"<head><meta content=\"always\" name=\"referrer\"></head>"
		"<TITLE>A Midsummer Night's Dream</TITLE>"
		"</html>";
		
	XmlParser p(xml);
	p.Relaxed();
	try {
		while(!p.IsTag())
			p.Skip();
		p.PassTag("html");
		while(!p.IsEof())
			if(p.Tag("head"))
			{
				while(!p.End("head")) {
					if(p.Tag("meta")) {
						DUMP(p["content"]);
					}
					else
						p.Skip();
				}
			}
			else
			if(p.Tag("TITLE")) {
				String TITLE = p.ReadText();
				DUMP(TITLE);
				p.PassEnd();
			}
			else
				p.Skip();
	}
	catch(XmlError e) {
		LOG("ERROR: " << e);
	}
	
	LOG("---- Done");
}


 
Read Message
Read Message
Read Message
Previous Topic: Missing ArrayIndex and AIndex
Next Topic: about some class equivalent to BOOST
Goto Forum:
  


Current Time: Tue May 13 23:41:11 CEST 2025

Total time taken to generate the page: 0.00703 seconds