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 » Community » Newbie corner » XML exercise
XML exercise [message #35990] Mon, 16 April 2012 20:49 Go to next message
idkfa46 is currently offline  idkfa46
Messages: 155
Registered: December 2011
Experienced Member
Good evening guys,
I can’t understand how can I correctly use XML. Looking into bazar I haven’t found a examples so I thought about a little “exercise” maybe useful for newbies about Parsing and XmlNode

Here a little example of what I’m planning to create:
index.php?t=getfile&id=3734&private=0

This little program have a droplist where users can select different type of cars. For each car, the program show select1, select2 and select3 values.

A possible Xml structure could be:

<car>A</car>
<value>
<Select1 value="1"/>
<Select2 value="2"/>
<Select3 value="3"/>
</value>
<car>B</car>
<value>
<Select1 value="4"/>
<Select2 value="5"/>
<Select3 value="6"/>
</value>

So, Selecting “A” from the DropList menu the program will show
Select1 = 1,
Select2 = 2,
Select3 =3,
and selecting “B”
Select1 = 4,
Select2 = 5,
Select3 = 6.


-The first step will be to code it for reading XLM structure and show stored values.

-Then, the second step will be to allow user adding/deleting cars with their own values.

The AddressBook example in bazar show the possibility to store datas but it creates a new xml everytime, so the problem is a bit different.


Thanks for your support
Regards,
Matteo

here the layout
LAYOUT(prova4Layout, 188, 240)
	ITEM(Label, dv___0, SetLabel(t_("Car :")).LeftPosZ(10, 55).TopPosZ(40, 21))
	ITEM(Label, dv___1, SetLabel(t_("Select 1 :")).LeftPosZ(10, 55).TopPosZ(108, 21))
	ITEM(Label, dv___2, SetLabel(t_("Select 2 :")).LeftPosZ(10, 55).TopPosZ(134, 21))
	ITEM(Label, dv___3, SetLabel(t_("Select 3 :")).LeftPosZ(10, 55).TopPosZ(162, 21))
	ITEM(DropList, dl, LeftPosZ(72, 103).TopPosZ(41, 19))
	ITEM(StaticText, dv___5, SetText(t_("Setting :")).SetFont(StdFontZ(16).Bold()).LeftPosZ(10, 166).TopPosZ(7, 25))
	ITEM(StaticText, dv___6, SetText(t_("Details :")).SetFont(StdFontZ(16).Bold()).LeftPosZ(10, 164).TopPosZ(74, 25))
	ITEM(EditDouble, select1, LeftPosZ(72, 102).TopPosZ(110, 19))
	ITEM(EditDouble, select2, LeftPosZ(72, 102).TopPosZ(136, 19))
	ITEM(EditDouble, select3, LeftPosZ(72, 102).TopPosZ(163, 19))
	ITEM(Button, dv___10, SetLabel(t_("NEW")).LeftPosZ(7, 77).TopPosZ(200, 23))
	ITEM(Button, dv___11, SetLabel(t_("DELETE")).LeftPosZ(93, 79).TopPosZ(200, 23))
END_LAYOUT
  • Attachment: Cattura.JPG
    (Size: 19.57KB, Downloaded 460 times)

[Updated on: Wed, 18 April 2012 09:23]

Report message to a moderator

Re: XML exercise [message #36003 is a reply to message #35990] Wed, 18 April 2012 09:18 Go to previous messageGo to next message
idkfa46 is currently offline  idkfa46
Messages: 155
Registered: December 2011
Experienced Member
Added a possible XML structure and modified post to be clearer... ( I hope)

Regards,
Matteo

[Updated on: Wed, 18 April 2012 09:25]

Report message to a moderator

Re: XML exercise [message #36080 is a reply to message #35990] Tue, 24 April 2012 21:06 Go to previous messageGo to next message
idkfa46 is currently offline  idkfa46
Messages: 155
Registered: December 2011
Experienced Member
I try to enliven this topic...
here my code to answer the first question about reading XLM structure and show stored values.

bool prova4::Load(const char *xml)
{
	try {
		XmlParser p(xml);
		while(!p.IsTag())
			p.Skip();
		int i =0;
		p.PassTag("prova");
		while(!p.End())
			if(p.Tag("car"))	
			{
				String car = p.ReadText();
				dl.Add(i, car);
				if(car == carDefault)
				{
					dl.SetIndex(i);
					loadSelect(xml, car);
				}
				i++;
				p.End();
			}
			else
			{
				p.Skip();
			}
		return true;
		}
	catch(XmlError e) 
	{
		Exclamation("XML error: " + e);
		return false;	
	}
}

bool prova4::loadSelect(const char *xml, String car)
{
	try {
		XmlParser p(xml);
		while(!p.IsTag())
			p.Skip();
		p.PassTag("prova");
		while(!p.End())
			if(p.Tag("car"))
				if(p.ReadText() == dl.GetValue())
				{
					p.End();
					p.PassTag("value");	
					while(!p.End())
							if(p.TagE("Select1"))
								select1 = ScanDouble(p["value"]);
							else 
							if(p.TagE("Select2"))
								select2 = ScanDouble(p["value"]);
							else
							if(p.TagE("Select3"))
								select3 = ScanDouble(p["value"]);			
				}
				else
				{
					p.End();
					p.PassTag("value");
					p.SkipEnd();
				}
		return true;
	}
	catch(XmlError e) 
	{
		Exclamation("XML error: " + e);
		return false;	
	}
}


dialog.xml
<?xml version="1.0"?>

<prova>
<car>A</car>
<value>
	<Select1 value="1"/> 
	<Select2 value="2"/> 
	<Select3 value="3"/>
</value>
<car>B</car>
<value>
	<Select1 value="4"/> 
	<Select2 value="5"/> 
	<Select3 value="6"/>
</value>
<car>C</car>
<value>
	<Select1 value="7"/> 
	<Select2 value="8"/> 
	<Select3 value="9"/>
</value>
<car>D</car>
<value>
	<Select1 value="10"/> 
	<Select2 value="11"/> 
	<Select3 value="12"/>
</value>
</prova>


Is this the right way ? any advice on it?

Regards,
Matteo
Re: XML exercise [message #36090 is a reply to message #36080] Thu, 26 April 2012 15:49 Go to previous messageGo to next message
Lance is currently offline  Lance
Messages: 526
Registered: March 2007
Contributor
Quote:


The AddressBook example in bazar show the possibility to store datas but it creates a new xml everytime, so the problem is a bit different.



The example has everything you need to do what you want. You can skip the FileSel dialog part and insert a fixed file name so that it doesn't "create a new xml everytime" (if I got it right).

If I do it, XML is good for storage and transportation of information beyond your application. Within your application, you should be better off to store it in a, eg sorted Vector, for better performance.

so:
1. program start, read(parse) XML database, and store car info in a more accessible way;

2. do add/edit/delete as you do in any other data manipulation application. XML is not relevant here

3. when close or explicit save requested, check if data is changed, if yes, save to XML.

you can already do 1 and 3 with reference to AddressXML, so just figure out how you should do 2.

HTH
Re: XML exercise [message #36182 is a reply to message #36090] Wed, 09 May 2012 12:27 Go to previous message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
but isn't it the false structure of xml should be used...?

I believe this will be a better one:

<vehicle>
  <car>A</car>
  <value num="1">10</value>
  <value num="2">11</value>
  <value num="3">12</value>
</vehicle>

OR
<vehicle>
  <car>A</car>
  <values>
    <a>10</a>
    <b>11</b>
    <c>12</c>
  </values>
</vehicle>


depending what you want to realize...

just my two cents
Previous Topic: fills the DropList from VectorMap
Next Topic: Search for constant and show it....
Goto Forum:
  


Current Time: Thu Mar 28 20:56:42 CET 2024

Total time taken to generate the page: 0.01274 seconds