Need help with atypical XML lazarus / 21-General ---------------------------------------------------------------------- krexon · 2021-04-27 13:53 I have atypical XML file, in which invoices are one by one. Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   1      AAA       BBB       CCC      2      BBB       CCC       DDD     3     BBB      DDD      AAA      I can easily get what I need, when XML contains data from only one invoice. I have no idea how to get 'rows' assigned to 'nr', when there are invoices one by one like in above example. I use XMLRead and DOM, but I can use something different. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-28 03:32 Hey Krexon, You'll need a better answer than mine, for sure, since I haven't used DOM a lot, just seen code on it. Most of the libraries that read XML have the concept of children/siblings. Siblings are all the children of a certain node. With this information I guess you can traverse that HORRIBLY structured XML with this pseudo code: - Using a for loop to iterate on the Children array Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  index: Integer;  child: TDOMTAG; // <-- Replace with the correct TObject ;)  InvoiceNumber: Integer = -1;  Invoice: TMyInvoiceClass;begin  for index:=0 to Pred(TagINV.Children.Count) do  begin    child:= TagINV.Children[index];    if child.TagName = 'nr' then    begin      if InvoiceNumber <> StrToInt(child.Text) then      begin        Invoice:= TMyInvoiceClass.Create;        Invoice.Number:= StrToInt(child.Text);      end;    end;    if child.TagName = 'rows' then    begin      // Read the whole set of rows and add it to Invoice.Rows    end;  end;end; - There's also the FirstChild and NextSibling approach, if DOM tag has it Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  sibling: TDOMTAG; // <-- Replace with the correct TObject ;)  InvoiceNumber: Integer = -1;  Invoice: TMyInvoiceClass;begin  sibling:= TagINV.FirstChild; // <-- or FirstSibling, need you to find out.  repeat    if sibling.TagName = 'nr' then    begin      if InvoiceNumber <> StrToInt(sibling.Text) then      begin        Invoice:= TMyInvoiceClass.Create;        Invoice.Number:= StrToInt(sibling.Text);      end;    end;    if sibling.TagName = 'rows' then    begin      // Read the whole set of rows and add it to Invoice.Rows    end;    sibling := sibling.NextSibling;  until sibling = nil;end; REMEMBER that this is all pseudo code and you need to find out if a DOM tag object supports an array of Children or just the NextSibling approach. This is a very simple example and misses a lot of checks to get the correct data out of the XML. It also doesn't account for a situation where that, already horrible, XML gets more strange Cheers, Gus ---------------------------------------------------------------------- krexon · 2021-04-28 06:33 Thank you, I'll look at it ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-28 06:51 Hey Krexon, Quote from: krexon on April 28, 2021, 08:33:27 amThank you, I'll look at it No problemo mate, you're quite welcome Cheers, Gus ---------------------------------------------------------------------- wp · 2021-04-28 10:00 Find tested sample code in the attachment. There are probably shorter solutions, but this code is a very basic application of the xml and dom methods provided and definitely works with the standard units without installation of further libraries. Read the https://wiki.lazarus.freepascal.org/XML_Tutorial for further information. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-28 11:07 Hey Krexon, I'm not trying to overstep on WP's code, but since I gave you pseudo code for both the Siblings and Children, I wanted to take the opportunity that WP has implemented the Siblings approach and I'm giving you the Children approach. Well, a mixed one since I got lazy and copy/pasted the inner cycle for the rows from WP But you'll get the gist from my outer cycle for the invoices. And I also had a nice play with some of the stuff I'm learning from this in terms of good UI design. Cheers, Gus ---------------------------------------------------------------------- krexon · 2021-04-28 13:21 Thank you very much. ---------------------------------------------------------------------- Gustavo 'Gus' Carreno · 2021-04-28 14:00 Hey Krexon, Quote from: krexon on April 28, 2021, 03:21:49 pmThank you very much. You're quite welcome, it was my pleasure to help Cheers, Gus