|
FORUMS PROFESSIONNELS WINDEV, WEBDEV et WINDEV Mobile |
| | | | | |
XML: How to read child nodes of a parent? |
Débuté par Wim, 12 oct. 2020 16:35 - 3 réponses |
| |
| | | |
|
| |
Posté le 12 octobre 2020 - 16:35 |
NaamXMLBestand is string nBeleningen is numeric 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 |
| |
| |
| | | |
|
| | |
| |
Membre enregistré 22 messages Popularité : +2 (2 votes) |
|
Posté le 13 octobre 2020 - 04:41 |
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 |
| |
| |
| | | |
|
| | |
| |
Posté le 13 octobre 2020 - 11:31 |
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
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 |
| |
| |
| | | |
|
| | |
| |
Membre enregistré 22 messages Popularité : +2 (2 votes) |
|
Posté le 13 octobre 2020 - 14:10 |
I'm glad you got it to work.
Best Regards, Geoff |
| |
| |
| | | |
|
| | | | |
| | |
| | |
| |
|
|
|