BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TXMLDocument style

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi XML
View previous topic :: View next topic  
Author Message
Marc Pelletier
Guest





PostPosted: Sat Jan 27, 2007 2:02 am    Post subject: TXMLDocument style Reply with quote



Hello,

I am reluctanctly allowing myself to be dragged into a small corner of
the xml world, which I have passively resisted up to now.

I'm a little confused about using TXMLDocument efficiently and clearly.

In my case I am writing a kml file for Google Earth. The result should
look like this:
<?xml version='1.0' encoding='utf-8'?>
<kml xmlns='http://earth.google.com/kml/2.0'>
<Document>
<Style id='default'>
<PolyStyle>
<color>44ff0000</color>
<fill>1</fill>
<outline>1</outline>
</PolyStyle>
<LineStyle>
<width>6</width>
<color>ffff0000</color>
</LineStyle>
</Style>
<Placemark>
<styleUrl> #default</styleUrl>
<MultiGeometry>
<Polygon id="poly1">
<!-- specific to Polygon -->
<extrude>0</extrude> <!-- boolean -->
<tessellate>1</tessellate> <!-- boolean -->
<altitudeMode>relativeToGround</altitudeMode>
<!-- kml:altitudeModeEnum: clampToGround,
relativeToGround, or absolute -->
<outerBoundaryIs>
<LinearRing>
<coordinates>
-104.1, 42.7, 1500
-104.2, 42.7, 1500
-104.2 42.9, 1500
-104.1, 42.9, 1500
-104.1, 42.7, 1500
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
</Document>
</kml>


My code so far looks like this:
XMLDoc := TXMLDocument.Create( Application );
XMLDoc.Options := XMLDoc.Options + [doNodeAutoIndent];
XMLDoc.Active := true;
XMLDoc.Version := '1.0';
i_XMLDoc := XMLDoc.CreateNode( 'kml', ntElement,
'xmlns=''http://earth.google.com/kml/2.0''') ;
XMLDoc.DocumentElement := i_XMLDoc;
i_XMLNode := i_XMLDoc.AddChild('Document');
with i_XMLNode.addChild('Style') do
begin
Attributes['id']='default';
with AddChild('PolyStyle') do
begin
with AddChild('color') do
Text := btnFillColor.HTMLColor;
with AddChild('fill') do
Text := '1';
with AddChild('outline')do
Text := '1';
end;
with AddChild('LineStyle') do
begin
with AddChild('width') do
Text := '6';
with AddChild('color') do
Text := btnPolyColor.HTMLColor;
end;
end;
with i_XMLNode.AddChild('Placemark') do
begin
with AddChild('styleUrl') do
Text := '#default';
with AddChild('MultiGeometry') do
begin
with AddChild('Polygon') do
begin
Attributes['id'] := inttostr(i);
end;
end;
end;
XMLDoc.SaveToFile( 'c:\temp\poly.xml' );

The only way around the with statements seems to be a whole bunch of
temporary variables. Both solutions are ugly, I think.

Surely there is a better way to do it?? Can someone illuminate me please?

thanks

Marc Pelletier
Goldak Airborne Surveys
Back to top
Harley Pebley
Guest





PostPosted: Sat Jan 27, 2007 3:36 am    Post subject: Re: TXMLDocument style Reply with quote



Quote:
My code so far looks like this:
...
with i_XMLNode.addChild('Style') do
begin
Attributes['id']='default';
with AddChild('PolyStyle') do
begin
with AddChild('color') do
Text := btnFillColor.HTMLColor;
with AddChild('fill') do
Text := '1';
with AddChild('outline')do
Text := '1';
end;
with AddChild('LineStyle') do
begin
with AddChild('width') do
Text := '6';
with AddChild('color') do
Text := btnPolyColor.HTMLColor;
end;
end;
with i_XMLNode.AddChild('Placemark') do
begin
with AddChild('styleUrl') do
Text := '#default';
with AddChild('MultiGeometry') do
begin
with AddChild('Polygon') do
begin
Attributes['id'] := inttostr(i);
end;
end;
end;

The only way around the with statements seems to be a whole bunch of
temporary variables. Both solutions are ugly, I think.

Surely there is a better way to do it?? Can someone illuminate me
please?

For those places where you're only using the value once, you could use
the result directly. I.e.

with i_XMLNode.addChild('Style') do
begin
Attributes['id'] := 'default';
with AddChild('PolyStyle') do
begin
AddChild('color').Text := btnFillColor.HTMLColor;
AddChild('fill').Text := '1';
AddChild('outline').Text := '1';
end;
with AddChild('LineStyle') do
begin
AddChild('width').Text := '6';
AddChild('color').Text := btnPolyColor.HTMLColor;
end;
end;
with i_XMLNode.AddChild('Placemark') do
begin
AddChild('styleUrl').Text := '#default';
with AddChild('MultiGeometry').AddChild('Polygon') do
begin
Attributes['id'] := inttostr(i);
AddChild('extrude').Text := '0';
...
end;
end;

Or perhaps you can approach the problem differently. Here's a couple
thoughts...

Depending on how you're getting the data to populate this, you might be
able to write a routine which does this in a more generic manner. For
example, if you have the data already in a tree-like data structure,
you might be able to write a recursive routine which builds the XML
nodes from the existing tree. Or perhaps you could put your data in a
structure which lends itself to easier automatic generation of this
output.

I see you have some of the data coming from things called btnXXXColor.
If this is from a GUI and its design lends itself to this, you might be
able to create a base frame which has a method which takes an XML node
as input. It can then create a child node based on some of its
properties. You then create different instances or descendants,
depending on what you're doing, which create the specific types of
nodes needed. Then to create the XML document, you have a method which
does the setup stuff (e.g. setting the namespace) and then passes it to
each frame which is responsible for filling out its part of the XML.

Do these make sense?

Hope it helps,
Harley Pebley
www.skylark-software.com
Back to top
Harley Pebley
Guest





PostPosted: Sat Jan 27, 2007 3:39 am    Post subject: Re: TXMLDocument style Reply with quote



Quote:
I am reluctanctly allowing myself to be dragged into a small corner
of the xml world, which I have passively resisted up to now.

I'm a little confused about using TXMLDocument efficiently and
clearly.

In my case I am writing a kml file for Google Earth. The result
should look like this: ...

My code so far looks like this: ...

Also, I don't think this is really an XML specific question. I see it
as more of a general design issue which happens to be applied to an
object dealing with XML. You might try asking in the oodesign group;
you might get some other perspectives.

Cheers,
Harley Pebley
www.skylark-software.com
Back to top
Andrea Raimondi
Guest





PostPosted: Wed Jan 31, 2007 8:26 pm    Post subject: Re: TXMLDocument style Reply with quote

Marc Pelletier ha scritto:
Quote:
Surely there is a better way to do it?? Can someone illuminate me please?

Use the XML Binding Wizard.

Quote:
thanks

Welcome,

Quote:
Marc Pelletier
Goldak Airborne Surveys

Andrew
Back to top
Marc Pelletier
Guest





PostPosted: Thu Feb 01, 2007 2:31 am    Post subject: Re: TXMLDocument style Reply with quote

Andrea Raimondi <rainaple (AT) tin (DOT) it> wrote in news:45c0a6d9
@newsgroups.borland.com:

Quote:
Use the XML Binding Wizard.


This sounds promising but I can't find it! How do I start the darn thing?

Thanks

Marc Pelletier
Back to top
Don Siders
Guest





PostPosted: Thu Feb 01, 2007 6:42 am    Post subject: Re: TXMLDocument style Reply with quote

Quote:
Use the XML Binding Wizard.
This sounds promising but I can't find it! How do I start the darn thing?

It's covered in the documentation (BDS2006):

ms-help://borland.bds4/bds4win32devguide/html/xmlusingthexmldatabindingwizard.htm

hth...
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi XML All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.