PC SOFT

PROFESSIONAL NEWSGROUPS
WINDEVWEBDEV and WINDEV Mobile

Home → WINDEV 2024 → XML: How to read child nodes of a parent?
XML: How to read child nodes of a parent?
Started by Wim, Oct., 12 2020 4:35 PM - 3 replies
Posted on October, 12 2020 - 4:35 PM
NaamXMLBestand is string // Declaratie string voor naam XML bestand
nBeleningen is numeric // Beleningen per relatie
ImportBelening is xmlDocument //

NaamXMLBestand = CompleteDir(fExeDir()) + "MAILBELENING.XML"
ImportBelening = XMLOpen(NaamXMLBestand,fromFile)

RelatieRec is xmlNode,Description = "ImportBelening.GROEPRELATIES.Relatie"
BeleningRec is xmlNode,Description = "ImportBelening.GROEPRELATIES.Relatie.GROEPBELENINGEN"
TekstRec is xmlNode,Description = "ImportBelening.GROEPRELATIES.Relatie.Tekst"

IF ImportBelening..RootNode..Occurrence < 1 THEN
Info("Het XML bestand bevat geen inhoud of heeft een verkeerde structuur. " + ErrorInfo())
ELSE
FOR EACH RelatieRec OF ImportBelening.GROEPRELATIES
RelatieRecord()
nBeleningen = 0

FOR EACH BeleningRec OF ImportBelening.GROEPRELATIES.Relatie.GROEPBELENINGEN
nBeleningen = nBeleningen + 1
BeleningRecord()
END
HAdd(MailBelening)
END
END


Part of the xml file:

<?xml version="1.0" encoding="UTF-8"?>
<GROEPRELATIES>
<Relatie>
<Mailadres>chadam@hollendekrakerstier.nl</Mailadres>
<Relatieid>1</Relatieid>
<Naam>Dundee</Naam>
<Voornaam>John</Voornaam>
<Voorletters>J</Voorletters>
<Voorvoegsel/>
<Geboortedatum>12/04/1992</Geboortedatum>
<Geboorteplaats/>
<Geslacht>M</Geslacht>
<Adres>Highstreet 10</Adres>
<PCWoonplaats>1000AA Amsterdam</PCWoonplaats>
<eMail>dundee@holowkraker.nl</eMail>
<GROEPBELENINGEN>
<Belening>
<Beleningsnr>B2000014</Beleningsnr>
<Beleensom>500</Beleensom>
<Vervaldatum>18/08/2020</Vervaldatum>
<Beleningsdatum>18/06/2020</Beleningsdatum>
</Belening>
<Belening>
<Beleningsnr>B2000027</Beleningsnr>
<Beleensom>51</Beleensom>
<Vervaldatum>26/08/2020</Vervaldatum>
<Beleningsdatum>26/06/2020</Beleningsdatum>
</Belening>
</GROEPBELENINGEN>

The XML file has many RelatieRec 's.
Every RelatieRec has one or more BeleningRec as subnodes.

The above structure reads every RelatieRec correct.
But for every RelatieRec the BeleningRec's of the first RelatieRec are read.

How to read the child nodes 'Belening' of a parent 'Relatie' in this case?

Kind regards, Wim
Registered member
22 messages
Popularité : +2 (2 votes)
Posted on October, 13 2020 - 4:41 AM
Hi Wim,

In "Project Explorer", under "External descriptions", I right-clicked and imported a copy of an appropriate xml file so I could refer to it in the xmlDocument description. In my case it was "Example", but I didn't have anything to work with using your code.

In any event I tried to substitute the node names etc that I used in a working example of mine, to the names of your nodes. I did get a little lost with the names used in your language but hopefully the following will work for you or at least put you on the right track.

NaamXMLBestand is string
NaamXMLBestand = fSelect("Documents", "File.xml", "Select an XML file","XML file (*.xml)" + TAB + "*.xml" + CR + "All" + TAB + "*.*", "xml")

IF NaamXMLBestand <> "" THEN
// Open the XML file
Trace(NaamXMLBestand)
ImportBelening is xmlDocument <description="Example">
ImportBelening = XMLOpen(NaamXMLBestand, fromFile)

RelatieRec is an xmlNode, description="ImportBelening.GROEPRELATIES.Relatie"
BeleningRec is an xmlNode, description="ImportBelening.GROEPRELATIES.Relatie.GROEPBELENINGEN"
n is int

FOR EACH RelatieRec OF ImportBelening.GROEPRELATIES ON Relatie
Trace(RelatieRec.RelatieNumber)
n += 1
Trace(n)
FOR EACH BeleningRec OF ImportBelening.GROEPRELATIES.Relatie[n] ON GROEPBELENINGEN
Trace(BeleningRec.Beleningsnr +", and date: " + BeleningRec.Vervaldatum)
END
END

END

Best Regards,
Geoff
Posted on October, 13 2020 - 11:31 AM
Hi Geoff,

Thank you very much for the time and effort you took for the example!

When I leave out the "ON Relatie and ON GROEPBELENINGEN" statements it works perfectly! (see below)

I am now going to study your code to understand why the small adjustment makes such a big difference.

Thanks again Geoff!

NaamXMLBestand is string
NaamXMLBestand = fSelect("Documents", "File.xml", "Select an XML file","XML file (*.xml)" + TAB + "*.xml" + CR + "All" + TAB + "*.*", "xml")

IF NaamXMLBestand <> "" THEN
// Open the XML file

ImportBelening is xmlDocument <Description="MAILBELENING.XML">
ImportBelening = XMLOpen(NaamXMLBestand, fromFile)
Trace(NaamXMLBestand)

RelatieRec is an xmlNode, Description = "ImportBelening.GROEPRELATIES.Relatie"
BeleningRec is an xmlNode, Description = "ImportBelening.GROEPRELATIES.Relatie.GROEPBELENINGEN"
n is int

FOR EACH RelatieRec OF ImportBelening.GROEPRELATIES
Trace(RelatieRec.Naam)
n += 1
Trace(n)
RelatieRecord()

FOR EACH BeleningRec OF ImportBelening.GROEPRELATIES.Relatie[n].GROEPBELENINGEN
Trace(BeleningRec.Beleningsnr +", and date: " + BeleningRec.Vervaldatum)
END
END

END
Registered member
22 messages
Popularité : +2 (2 votes)
Posted on October, 13 2020 - 2:10 PM
I'm glad you got it to work. :)

Best Regards,
Geoff