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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting data out of a memo field 1

Status
Not open for further replies.

PaidtheUmpire

Programmer
Jan 4, 2004
105
AU
I thank those whom help me out greatly. :p

Anyways... i have text that is coming into a memo field like so...


Code:
...

        <extensions>
          <gpxx:WaypointExtension xmlns:gpxx="[URL unfurl="true"]http://www.garmin.com/xmlschemas/GpxExtensions/v2"[/URL] xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xsi:schemaLocation="[URL unfurl="true"]http://www.garmin.com/xmlschemas/GpxExtensions/v2[/URL] [URL unfurl="true"]http://www.garmin.com/xmlschemas/GpxExtensions/v2/GpxExtensionsv2.xsd">[/URL]
            <gpxx:DisplayMode>SymbolOnly</gpxx:DisplayMode>
          </gpxx:WaypointExtension>
        </extensions>
      </rtept>
      </rte>

      <wpt lat="-26.5182" lon="152.5146">
      <time>2005-08-12T18:26:38Z</time>
      <name>16-11-2005 123000</name>
      <extensions>
        <gpxx:WaypointExtension xmlns:gpxx="[URL unfurl="true"]http://www.garmin.com/xmlschemas/GpxExtensions/v2"[/URL] xmlns:xsi="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema-instance"[/URL] xsi:schemaLocation="[URL unfurl="true"]http://www.garmin.com/xmlschemas/GpxExtensions/v2[/URL] [URL unfurl="true"]http://www.garmin.com/xmlschemas/GpxExtensions/v2/GpxExtensionsv2.xsd">[/URL]
          <gpxx:DisplayMode>SymbolAndName</gpxx:DisplayMode>
        </gpxx:WaypointExtension>
      </extensions>
      </wpt>

...

What i want to be able to do is extract out the following bits of data... the numbers in the first line and the bit of data between then <name> and </name> tags.

In this piece of data...
A: -26.5182
B: 152.5146
C: 16-11-2005 123000

Any ideas?

Delphi, Delphi, Delphi. Oi! Oi! Oi!
 
You could write your own parsing routine using Pos and a couple of other string-handling routines. But it looks to me like you're dealing with an XML file, so you might be better off using TXMLDocument (Internet tab of the palette) if it's available in your component set. This will allow you to easily grab data for any element and attribute using built in functions.

If I was parsing it with my own custom routine, I would load the file into a TRichEdit because it has a FindText routine and the handy SelStart, SelText and SelLength (that are available in TMemo). A combination of these facilities makes parsing text quite easy. If you need more pointers just let us know.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Here you go:
Code:
function GetSLength(Text: string; StartPos: integer): integer;
var i: integer;
begin
Delete(Text,1,StartPos);

for i := 1 to Length(Text) do
        begin
        if Text[i] = '"' then
                begin
                Result := i;
                Break;
                end;
        end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var sLat, sLon, sName: string;
startPos: integer;
begin
// Get Lat value
startPos := Pos('lat="',Memo1.Text) + 5;
sLat := Copy(Memo1.Text, startPos, GetSLength(Memo1.Text,startPos));

// Get Lon value
startPos := Pos('lon="',Memo1.Text) + 5;
sLon := Copy(Memo1.Text, startPos, GetSLength(Memo1.Text,startPos));

// Get name value
startPos := Pos('<name>',Memo1.Text) + 6;
sName := Copy(Memo1.Text,startPos, Pos('</name>',Memo1.Text) - startPos);

ShowMessage(sLat + #10#13 + sLon + #10#13 + sName);
end;

This'll get exactly those values (provided they are the
first ones that are encountered in the document of course)

[bobafett] BobbaFet [bobafett]

Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
faq102-5352
 
BobbaFet...

a quick question about your code:
Why does the lines:
Code:
startPos := Pos('lat="',Memo1.Text) + 5;
Code:
startPos := Pos('lon="',Memo1.Text) + 5;
Code:
startPos := Pos('<name>',Memo1.Text) + 6;

have the "+5" or "+6" at the end of it?


Delphi, Delphi, Delphi. Oi! Oi! Oi!
 
I'll answer that one for you, it's because the Pos function will return the starting position of the string specified, whereas you want to read what comes after it.

e.g. in the following string:

lat="-26.5182"

if 'lat="' is found at position 1 (i.e. the l is position 1) then the minus sign is at position 6

Steve
 
Steve nailed it :)

[bobafett] BobbaFet [bobafett]

Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
faq102-5352
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top