Difficulty parsing XML using a TXMLDocument

361 Views Asked by At

I have not used XML before, and am having difficulty parsing XML using a TXMLDocument. (Delphi 10.1)

In my app I need to find placename and postcode etc. of where a photograph was taken using the Lat and Long from the EXIF information by doing a reverse geocoding query at Geonames Web Service - see http://www.geonames.org/export/web-services.html

This part works fine, but am having trouble getting the data from the returned XML, which comes in the form of

<?xml version="1.0" standalone="no"?>
<geonames>
    <code>
        <postalcode>18500</postalcode>
        <name>Guadix</name>
        <countryCode>ES</countryCode>
        <lat>37.29932</lat>
        <lng>-3.13922</lng>
        <adminCode1>AN</adminCode1>
        <adminName1>Andalucia</adminName1>
        <adminCode2>GR</adminCode2>
        <adminName2>Granada</adminName2>
        <adminCode3/>
        <adminName3/>
        <distance>0.58142</distance>
    </code>
</geonames>

I am trying to put this data into a record:

TGeonamesPostalcodeData = record
    gpcPostalcode: string;
    gpcName: string;
    gpcCountryCode: string;
    gpcLat: string;
    gpcLng: string;
    gpcAdminCode1: string;
    gpcAdminName1: string;
    gpcAdminCode2: string;
    gpcAdminName2: string;
    gpcAdminCode3: string;
    gpcAdminName3: string;
    gpcDistance: string;
  end;

with this test function (in a DataModule with a TXMLDocument and a TIdHTTP)

function TdmGeonames.GetGeonamesPostalcodeData(dLatitude, dLongitude:
  Double; sUsername: string): TGeonamesPostalcodeData;
    const
  URL1: string = 'http://api.geonames.org/findNearbyPostalCodes?lat=';
  URL2: string = '&style=FULL&maxRows=1&username=';
var
  sURL: TFilename;
  s, sLat, sLng, sBuffer: string;
  StartItemNode, NodeElement: IXMLNode;
  sWidestring: WideString;
begin
  sLat := FloatToStrF(dLatitude, ffGeneral, 8, 4);
  sLng := FloatToStrF(dLongitude, ffGeneral, 8, 4);
  sURL := URL1 + sLat + '&lng=' + sLng + URL2 + sUsername;
  sBuffer := IdHTTPGeonames.Get(sURL);
  with XMLDocumentGeonames do
    begin
      LoadFromXML(sBuffer);
      Active := True;
      StartItemNode := ChildNodes.First.ChildNodes.FindNode('postalcode');
      // NodeElement := StartItemNode;
      Result.gpcPostalcode := StartItemNode.ChildNodes['postalcode'].Text;

end;

The last line causes an access violation. I am obviously doing something stupid but I can't see what.

As I said, I have never used XML data or TXMLDocument before. Any help most appreciated.

1

There are 1 best solutions below

0
On

Got it working.

Empty nodes were causing the access violation, now check each one isn't nil.

Also I should have gone to the root node, find it's child node then get the data is in child nodes of that node.

Thanks you @whosrdaddy and @Ssm for your help and pointing me in the right direction