Recent

Author Topic: Count the number of nodes in a xml  (Read 717 times)

folkeu08

  • Full Member
  • ***
  • Posts: 106
Count the number of nodes in a xml
« on: January 01, 2024, 11:10:08 am »
Hi the Team,
I am creating an xml file from a geocoding application.
When reading the xml, I would like to know the number of polygons present by counting the number of "<Placemark>" nodes present (5 in our test.xml file).
I started with the "Show node names" example from the page https://wiki.lazarus.freepascal.org/XML_Tutorial/fr but I get errors if I use my file.
I guess it doesn't work with my file because I still have other nodes under "Placemark".

Code: Pascal  [Select][+][-]
  1. var
  2.    Document: TXMLDocument;
  3.    i, j, K: Integer;
  4.   begin
  5.     ReadXMLFile(Document, 'test.kml');
  6.     Memo.Lines.Clear;
  7.     with Document.DocumentElement.ChildNodes do
  8.     //with Document.DocumentElement.ChildNodes.Item[1] do
  9.       begin
  10.         k := 0;
  11.         for i := 0 to (Count - 1) do
  12.           begin
  13.             Memo.Lines.Add(Item[i].NodeName + ' ' + Item[i].Attributes.Item[0].NodeValue);
  14.             for j := 0 to (Item[i].ChildNodes.Count - 1) do
  15.               begin
  16.                 Memo.Lines.Add(Item[i].ChildNodes.Item[j].NodeName + ' ' + Item[i].ChildNodes.Item[j].FirstChild.NodeValue);
  17.                 K := K+1;
  18.               end;
  19.           end;
  20.       end;
  21.    Showmessage(IntToStr(K));
  22.    Document.Free;
  23.  end;                                  
  24.  

The second step will be to insert the longitude and latitude values ​​for each polygon of each node into a SQLite table (or 2 if necessary). The goal is to draw these polygons using "lazmapviewer" without having to reread the relevant XML.

Thanks for your help as I'm lost with the terminology "Node" and "ChildNode". English is not my native language.
Francois

You must renam the file test.txt in test.kml (Thanks)

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: Count the number of nodes in a xml
« Reply #1 on: January 02, 2024, 06:43:09 am »
Hi,   
Here you have an example application that reads kml files and allows you to draw each polygon (placemark): https://forum.lazarus.freepascal.org/index.php/topic,64628.msg491620.html#msg491620
Best regards / Pozdrawiam
paweld

folkeu08

  • Full Member
  • ***
  • Posts: 106
Re: Count the number of nodes in a xml
« Reply #2 on: January 02, 2024, 04:06:04 pm »
Good morning,
Exactly, I'm trying to cheat by counting the number of items in your source's listBox but I can't get anything right.
I'm a complete beginner and have never used pointers.
I don't understand what the code does with Stax and so I'm going there haphazardly.
Francois

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: Count the number of nodes in a xml
« Reply #3 on: January 02, 2024, 07:27:13 pm »
SAX is a different way of reading XML files. It doesn't treat the xml as a tree of objects, but reads it like a text file telling you which node it is in. This is the most convenient way for me to read xml files in most cases.
Wanting to read the values, you have to indicate in the SAXStartElement and SAXEndElement routines from which location they are to be retrieved - that's what I use the access path for (attached is the application with which I retrieve them).
I also add to the attachment a modified application for reading KML files - I added information about placemark count and coordinate points count
If you have any questions, ask - I'll answer to the best of my ability
Best regards / Pozdrawiam
paweld

 

TinyPortal © 2005-2018