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 » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » Using DOM like XML parser
Using DOM like XML parser [message #6474] Mon, 13 November 2006 21:55 Go to next message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
I wanted to try DOM (Document Object Model) like XML processing with XmlNode class. I modified AddressBook example to use that.
Additionally I made some changes to use ArrayCtrl columns and XML in a more safe way.
Re: Using DOM like XML parser [message #6475 is a reply to message #6474] Mon, 13 November 2006 23:35 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
For fast reference, this is the new Open method, loading, parsing the XML file and putting its content to the ArrayCtrl:
void AddressBook::Open()
{
	if(!fs.ExecuteOpen()) return;
	filename = fs;
	array.Clear();
	try {
		String d = LoadFile(filename);
		XmlNode n = ParseXML(d);
		if(n.GetCount()==0 || n[0].GetTag() != "AddressBook")
			throw XmlError("No AddressBook tag in ");
		const XmlNode &ab = n[0];
		array.SetCount(ab.GetCount());
		for(int i=0; i < ab.GetCount(); i++){
			const XmlNode &person = ab[i];
			for(int j=0; j < person.GetCount(); j++){
				const XmlNode &prop = person[j];
				if(prop.IsTag(TAG_NAME)) array.Set(i, TAG_NAME, prop[0].GetText());
				else if(prop.IsTag(TAG_SURNAME)) array.Set(i, TAG_SURNAME, prop[0].GetText());
				else if(prop.IsTag(TAG_ADDRESS)) array.Set(i, TAG_ADDRESS, prop[0].GetText());
				else if(prop.IsTag(TAG_EMAIL)) array.Set(i, TAG_EMAIL, prop[0].GetText());
			}
		}
	}
	catch(XmlError &e) {
		Exclamation("Error reading the input file:&" + DeQtf(e) );
	}
}
Re: Using DOM like XML parser [message #6478 is a reply to message #6475] Tue, 14 November 2006 00:41 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Well, I must say you got me confused at first, but you are right - XmlNode is roughly equivalent of DOM...

Mirek
Re: Using DOM like XML parser [message #6480 is a reply to message #6478] Tue, 14 November 2006 00:59 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
But the example is not perfect - you should reimplement Save as well Wink

Mirek
Re: Using DOM like XML parser [message #6481 is a reply to message #6480] Tue, 14 November 2006 02:05 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
luzr wrote on Tue, 14 November 2006 00:59

But the example is not perfect - you should reimplement Save as well Wink



You are right Smile
void AddressBook::Save()
{
	if(IsEmpty(filename)) {
		SaveAs();
		return;
	}
	XmlNode n;
	XmlNode &ab = n.Add(TAG_ADDRESSBOOK);
	for(int i=0; i < array.GetCount(); i++)
	{
		XmlNode &person = ab.Add(TAG_PERSON);
		person.Add(TAG_NAME).Add().CreateText(array.Get(i, TAG_NAME));
		person.Add(TAG_SURNAME).Add().CreateText(array.Get(i, TAG_SURNAME));
		person.Add(TAG_ADDRESS).Add().CreateText(array.Get(i, TAG_ADDRESS));
		person.Add(TAG_EMAIL).Add().CreateText(array.Get(i, TAG_EMAIL));
	}
	if(!SaveFile(filename, AsXML(n)))
		Exclamation("Error saving the file!");
}

Re: Using DOM like XML parser [message #6494 is a reply to message #6481] Tue, 14 November 2006 15:02 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
With your permission, I will add it as examples/AddressBookXML2.

Mirek
Re: Using DOM like XML parser [message #6495 is a reply to message #6494] Tue, 14 November 2006 15:25 Go to previous message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
luzr wrote on Tue, 14 November 2006 15:02

With your permission, I will add it as examples/AddressBookXML2.


Thanks, maybe this example can help somebody Smile
Previous Topic: Groovey
Next Topic: An OpenGL ctrl for Linux / X11
Goto Forum:
  


Current Time: Fri Mar 29 09:39:40 CET 2024

Total time taken to generate the page: 0.01028 seconds