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 » Xmlize works only for storing
Xmlize works only for storing [message #18445] Wed, 01 October 2008 15:16 Go to next message
exhu is currently offline  exhu
Messages: 12
Registered: April 2008
Location: Belarus
Promising Member
Please, help understanding the XML serialization implemented in UPP.

This code perfectly saves the XML as intended, but does not load attribute values:
void ServData::save() {
    StoreAsXMLFile(*this);
}

void ServData::load() {
    LoadFromXMLFile(*this);
}

void ServData::Xmlize(XmlIO xml) {
    ::Xmlize(xml.Add("map"), locationMap);
}

/////////


void Xmlize(XmlIO xml, Map & locMap) {
    
    Xmlize(xml.Add("left"), locMap.left);    
    Xmlize(xml.Add("right"), locMap.right);
    Xmlize(xml.Add("top"), locMap.top);
    Xmlize(xml.Add("bottom"), locMap.bottom);
}

void Xmlize(XmlIO xml, MapPlace & place) {
    String nm;
    
	if (xml.IsStoring())
		nm = place.name;
    
    
    xml.Attr("name", nm); // nm is always empty on xml.IsLoading! why?
     
 	if (xml.IsLoading())
 		place.name = nm;
	
}
Re: Xmlize works only for storing [message #18454 is a reply to message #18445] Wed, 01 October 2008 22:27 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

I doubt if
Xmlize(xml.Add("***"), ***);

would work in both directions.

Why don`t you use construction from Xmlize reference sample:
void *****::*****::Xmlize(XmlIO xml)
{
	xml
		("***", ***)
		("***", ***)
	;
}

Re: Xmlize works only for storing [message #18464 is a reply to message #18454] Thu, 02 October 2008 09:08 Go to previous messageGo to next message
exhu is currently offline  exhu
Messages: 12
Registered: April 2008
Location: Belarus
Promising Member
Quote:

Why don`t you use construction from Xmlize reference sample:


I can't use it because STL and other simple types already defined in the program do not contain Xmlize methods which are called by the template.

Changed to:

void ServData::Xmlize(XmlIO xml) {
    ::Xmlize(XmlIO(xml,"map"), locationMap);
    //XmlIO(xml, "magic").Attr("magic", magic);
    //magic = magic;
}

/////////


void Xmlize(XmlIO xml, Map & locMap) {
    xml.Attr("shopname", locMap.shopName);
    Xmlize(XmlIO(xml, "left"), locMap.left);    
    Xmlize(XmlIO(xml,"right"), locMap.right);
    Xmlize(XmlIO(xml,"top"), locMap.top);
    Xmlize(XmlIO(xml,"bottom"), locMap.bottom);
    
}


Now it works, but it's not obvious why because both Add() method and XmlIO() constructor use & (reference) for variable argument.

Who can explain this magic? No comments at all in the library sources Sad
Re: Xmlize works only for storing [message #18473 is a reply to message #18464] Thu, 02 October 2008 19:22 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

[quote title=exhu wrote on Thu, 02 October 2008 11:08]
Quote:

I can't use it because STL and other simple types already defined in the program do not contain Xmlize methods which are called by the template.

Sorry I can`t clearly understand what are you talking about.
Re: Xmlize works only for storing [message #18482 is a reply to message #18473] Fri, 03 October 2008 08:37 Go to previous messageGo to next message
exhu is currently offline  exhu
Messages: 12
Registered: April 2008
Location: Belarus
Promising Member
Try compiling,

std::list<MyType> mylist;

XmlIO(xml, mylist);



And you'll get errors like "T.Xmlize: The class does not define a method Xmlize"...
Re: Xmlize works only for storing [message #18483 is a reply to message #18482] Fri, 03 October 2008 09:13 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

I see no critical problem with this. You may write your own class derived from std::list<...> with Xmlize function.
Besides I do not thnk it is good idea to mix STL and NTL libraries in code.
Re: Xmlize works only for storing [message #18486 is a reply to message #18483] Fri, 03 October 2008 11:54 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Mindtraveller wrote on Fri, 03 October 2008 03:13

I see no critical problem with this. You may write your own class derived from std::list<...> with Xmlize function.
Besides I do not thnk it is good idea to mix STL and NTL libraries in code.


Well, I certainly would not recomend using STL Smile but sometimes you perhaps need to deal with existing code...

You can define Xmlize as template function specialisation and that solves the problem of "external" types:

#include <Core/Core.h>
#include <vector>

using namespace Upp;
using namespace std;

template<> void Upp::Xmlize(XmlIO xml, vector<int>& data) {
	if(xml.IsStoring())
		for(int i = 0; i < (int)data.size(); i++)
			Xmlize(xml.Add("item"), data[i]);
	else {
		data.clear();
		for(int i = 0; i < xml->GetCount(); i++)
			if(xml->Node(i).IsTag("item")) {
				data.push_back(0);
				Xmlize(xml.At(i), data.back());
			}
	}
}

CONSOLE_APP_MAIN
{
	vector<int> x;
	x.push_back(1);
	x.push_back(2);
	x.push_back(3);
	String s = StoreAsXML(x, "std-test");
	DUMP(s);
	vector<int> y;
	LoadFromXML(y, s);
	for(int i = 0; i < (int)y.size(); i++)
		DUMP(y[i]);
}


Mirek

[Updated on: Fri, 03 October 2008 11:54]

Report message to a moderator

Re: Xmlize works only for storing [message #18487 is a reply to message #18486] Fri, 03 October 2008 11:57 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
PS.: added to reference examples...
Re: Xmlize works only for storing [message #18518 is a reply to message #18486] Mon, 06 October 2008 09:45 Go to previous messageGo to next message
exhu is currently offline  exhu
Messages: 12
Registered: April 2008
Location: Belarus
Promising Member
luzr wrote on Fri, 03 October 2008 12:54



Well, I certainly would not recomend using STL Smile




Ok, but are there analogues in NTL to the following STL classes:
set, list ?
Re: Xmlize works only for storing [message #18539 is a reply to message #18518] Tue, 07 October 2008 12:41 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
exhu wrote on Mon, 06 October 2008 03:45

luzr wrote on Fri, 03 October 2008 12:54



Well, I certainly would not recomend using STL Smile




Ok, but are there analogues in NTL to the following STL classes:
set, list ?


std::set -> Index. It provides something a bit more complex, but can easily replace set and multiset.

std::list is simply completely useless container. Use Vector/Array/BiVector/BiArray.

(Before you start argumenting about O(1) insertion times, tell how do you know where to insert Smile

Mirek
Previous Topic: FindFile, get dir list
Next Topic: String reverse find
Goto Forum:
  


Current Time: Tue Apr 23 18:47:07 CEST 2024

Total time taken to generate the page: 0.01600 seconds