Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create and parse complex XML file with delphi.

Status
Not open for further replies.

YamahaJojje

Programmer
May 27, 2004
11
SE
Hi!

First of I have to excuse my English. I haven't written a word in 4 years so its pretty rusty.

I have a "small" problem. I have to create a program to manage a carpool.

I want to make a program that stores info in an xml file.

The main form should contain a listview where the different cars are listed. And there you should click on a car to open a new form with info about the car.

The info I need to store is Car, Fuel consumption, Who drives it and service.

Best regards Johan
 
first engineer up your xml file.

delphi provides a way to autogenerate code based on the xml file contents.

what delphi version are you using?

the xml file layout could be something like this:

Code:
<?xml version="1.0"?>
<Cars>
  <Car Brand="Mercedes" Owner="john doe" Consumption="7" Service="30000"/>
  <Car Brand="Toyota" Owner="agent smith" Consumption="4" Service="90000"/>
  <Car Brand="Volvo" Owner="Mary poppins" Consumption="4" Service="1200000"/>
</Cars>

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I use Delphi 7 enterprise.

The problem is that i have to add alot of the same data too one car.

Like

Volvo
joe 100km and date
tim 120km and date
etc.

I dont really know how to explain in english.

/Jojje
 
it's just a matter how you engineer the xml file. it's the same as creating a database model.

based on what you said I would do this:

Code:
<?xml version="1.0"?>
<Cars>
  <Car Brand="Volvo"/>
   <Service>
    <Entry Owner="Joe" Km="100"/>
    <Entry Owner="Tim" Km="800"/>
   </Service>
  <Car Brand="Mercedes"/>
   <Service>
    <Entry Owner="Jack" Km="2000"/>
   </Service>
</Cars>

but maybe you are not telling everything.
make sure you model the xml file with all the details.
generating the code is simple once the xml file structure is well known.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
oops, the xml was wrong, forgot the car closing tags:

Code:
<?xml version="1.0"?>
<Cars>
  <Car Brand="Volvo">
   <Service>
    <Entry Owner="Joe" Km="100"/>
    <Entry Owner="Tim" Km="800"/>
   </Service>
  </Car>
  <Car Brand="Mercedes">
   <Service>
    <Entry Owner="Jack" Km="2000"/>
   </Service>
  </Car>
</Cars>

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
As the other posters have alluded to the XML Binding Wizard which will read your XML and give you an object that will let you access all of the entities very easily. Unfortunately if your schema is changing then I find the code that the tool writes could be confusing to edit. e.g. If your sample file doesn't indicate that an element can have multiple entities then it won't generate a structure to handle that for you. You can force it to do that, however.

I use for my XML projects. The only problem is that there isn't a great deal of documentation so getting started can be a challenge and you get to code the parsing and writing manually. There is a newsgroup available for help.
 
Djangman, I always keep a template xml in my project.
so if the schema changes, I edit the template and regenerate the code.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
On my first big XML project the files were large and several entities had the same name. :-( So when I tested with a few different files I had errors crop up. I would have loved to use the 'Delphi' way. Would have cut my development time in half. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top