LoloSept Posted April 16, 2024 Posted April 16, 2024 Good evening, I'm trying to read an XML file. I can read some of the data, but not all. I searched the forums, to no avail (understood how to do it). The goal is to read the XML file, search for text and retrieve data from this node. Here is my test code and XML file: expandcollapse popup;test xml com.au3 #include <Array.au3> ; Set the XML file $xmlpath = "PrixCarburants_test.xml" $oXml = ObjCreate("Msxml2.DOMDocument") $oXml.load($xmlpath) ; Fetch All Entities from XAML $objNodeList = $oXml.selectNodes("pdv_liste/pdv") $i = 0 For $node in $objNodeList $i = $i + 1 ConsoleWrite($i & " : " & $node.nodename & @CRLF) $objChildNodeList = $node.selectNodes("*") For $ChildNode in $objChildNodeList ConsoleWrite(@TAB & $ChildNode.nodename & ' = ' & $ChildNode.text & @CRLF) if $ChildNode.nodename = "GPLc" Then MsgBox(64, "Yes", "Yes") EndIf Next Next <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> <pdv_liste> <pdv id="51240002" latitude="4886100" longitude="448500" cp="51240" pop="R"> <adresse>RN44</adresse> <ville>Pogny</ville> <horaires automate-24-24="1"> <jour id="1" nom="Lundi" ferme="1"> <horaire ouverture="01.00" fermeture="01.00"/> </jour> <jour id="2" nom="Mardi" ferme="1"> <horaire ouverture="01.00" fermeture="01.00"/> </jour> <jour id="3" nom="Mercredi" ferme="1"> <horaire ouverture="01.00" fermeture="01.00"/> </jour> <jour id="4" nom="Jeudi" ferme="1"> <horaire ouverture="01.00" fermeture="01.00"/> </jour> <jour id="5" nom="Vendredi" ferme="1"> <horaire ouverture="01.00" fermeture="01.00"/> </jour> <jour id="6" nom="Samedi" ferme="1"> <horaire ouverture="01.00" fermeture="01.00"/> </jour> <jour id="7" nom="Dimanche" ferme="1"> <horaire ouverture="01.00" fermeture="01.00"/> </jour> </horaires> <services> <service>Laverie</service> <service>Piste poids lourds</service> <service>Vente de gaz domestique (Butane, Propane)</service> <service>Automate CB 24/24</service> <service>DAB (Distributeur automatique de billets)</service> </services> <prix nom="Gazole" id="1" maj="2024-04-06 05:42:21" valeur="1.789"/> <prix nom="E10" id="5" maj="2024-04-06 05:42:21" valeur="1.889"/> <prix nom="SP98" id="6" maj="2024-04-02 06:14:35" valeur="1.945"/> </pdv> <pdv id="38220002" latitude="4508300" longitude="576600" cp="38220" pop="R"> <adresse>AVENUE MAURICE THOREZ</adresse> <ville>VIZILLE</ville> <horaires automate-24-24="1"> <jour id="1" nom="Lundi" ferme=""> <horaire ouverture="06.00" fermeture="22.00"/> </jour> <jour id="2" nom="Mardi" ferme=""> <horaire ouverture="06.00" fermeture="22.00"/> </jour> <jour id="3" nom="Mercredi" ferme=""> <horaire ouverture="06.00" fermeture="22.00"/> </jour> <jour id="4" nom="Jeudi" ferme=""> <horaire ouverture="06.00" fermeture="22.00"/> </jour> <jour id="5" nom="Vendredi" ferme=""> <horaire ouverture="06.00" fermeture="22.00"/> </jour> <jour id="6" nom="Samedi" ferme=""> <horaire ouverture="06.00" fermeture="22.00"/> </jour> <jour id="7" nom="Dimanche" ferme=""> <horaire ouverture="07.00" fermeture="22.00"/> </jour> </horaires> <services> <service>Toilettes publiques</service> <service>Boutique alimentaire</service> <service>Boutique non alimentaire</service> <service>Restauration à emporter</service> <service>Restauration sur place</service> <service>Bar</service> <service>Station de gonflage</service> <service>Carburant additivé</service> <service>Piste poids lourds</service> <service>Lavage automatique</service> <service>Espace bébé</service> <service>Vente de gaz domestique (Butane, Propane)</service> <service>Vente d'additifs carburants</service> <service>Automate CB 24/24</service> </services> <prix nom="Gazole" id="1" maj="2024-04-12 03:44:13" valeur="1.832"/> <prix nom="E85" id="3" maj="2024-03-15 05:57:03" valeur="0.899"/> <prix nom="GPLc" id="4" maj="2024-01-01 08:00:00" valeur="0.920"/> <prix nom="E10" id="5" maj="2024-04-13 00:14:16" valeur="1.990"/> <prix nom="SP98" id="6" maj="2024-01-30 16:25:56" valeur="1.990"/> </pdv> </pdv_liste> Could you help me read the value of 'Price' (ex. price name="GPLc" and latitude="4508300" longitude="576600" cp="38220") THANKS!
Solution Andreik Posted April 16, 2024 Solution Posted April 16, 2024 (edited) expandcollapse popupLocal $oXML = ObjCreate('Microsoft.XMLDOM') If IsObj($oXML) Then $oXML.load("PrixCarburants_test.xml") $oXML.setProperty('SelectionLanguage', 'XPath') $oNodes = $oXML.selectNodes('/pdv_liste/pdv') For $oNode In $oNodes ConsoleWrite('Latitude: ' & $oNode.getAttribute('latitude') & @CRLF) ConsoleWrite('Longitude: ' & $oNode.getAttribute('longitude') & @CRLF) ConsoleWrite('CP: ' & $oNode.getAttribute('cp') & @CRLF) ConsoleWrite('Address: ' & $oNode.selectSingleNode('adresse').text & @CRLF) ConsoleWrite('Ville: ' & $oNode.selectSingleNode('ville').text & @CRLF) ConsoleWrite('Horaires: ' & @CRLF) $oJours = $oNode.selectNodes('horaires/jour') For $oJour In $oJours $oHoraire = $oJour.selectSingleNode('horaire') $sJour = $oJour.getAttribute('nom') $sOverture = $oHoraire.getAttribute('ouverture') $sFermeture = $oHoraire.getAttribute('fermeture') ConsoleWrite($sJour & ' (Ouverture: ' & $sOverture & ', Fermeture: ' & $sFermeture & @CRLF) Next $oServices = $oNode.selectNodes('services/service') For $oService In $oServices ConsoleWrite('Service: ' & $oService.text & @CRLF) Next $oPrixCol = $oNode.selectNodes('prix') For $oPrix In $oPrixCol $sPrixNom = $oPrix.getAttribute('nom') $sPrixId = $oPrix.getAttribute('id') $sPrixMaj = $oPrix.getAttribute('maj') $sPrixVal = $oPrix.getAttribute('valeur') ConsoleWrite('Prix: ' & $sPrixNom & ' (ID: ' & $sPrixId & ', Maj: ' & $sPrixMaj & ', Valeur: ' & $sPrixVal & @CRLF) Next ConsoleWrite(@CRLF) Next Else ConsoleWrite('Invalid XML object.' & @CRLF) EndIf Or a regex version: expandcollapse popup$sData = FileRead('PrixCarburants_test.xml') $aPDV = StringRegExp($sData, '(?is)(<pdv (?:.*?)<\/pdv>)', 3) If IsArray($aPDV) Then For $Index = 0 To UBound($aPDV) - 1 $vLatitude = StringRegExp($aPDV[$Index], '(?i)<pdv(?:.*?)latitude="(\d+)"(?:.*?)>', 3) If Not @error Then ConsoleWrite('Latitude: ' & $vLatitude[0] & @CRLF) $vLongitude = StringRegExp($aPDV[$Index], '(?i)<pdv(?:.*?)longitude="(\d+)"(?:.*?)>', 3) If Not @error Then ConsoleWrite('Longitude: ' & $vLongitude[0] & @CRLF) $vCP = StringRegExp($aPDV[$Index], '(?i)<pdv(?:.*?)cp="(\d+)"(?:.*?)>', 3) If Not @error Then ConsoleWrite('CP: ' & $vCP[0] & @CRLF) $vAdresse = StringRegExp($aPDV[$Index], '(?i)<adresse>(.*?)<\/adresse>', 3) If Not @error Then ConsoleWrite('Adresse: ' & $vAdresse[0] & @CRLF) $vVille = StringRegExp($aPDV[$Index], '(?i)<ville>(.*?)<\/ville>', 3) If Not @error Then ConsoleWrite('Ville: ' & $vVille[0] & @CRLF) ; Horaires $aHoraires = StringRegExp($aPDV[$Index], '(?is)(<jour.*?<\/jour>)', 3) If IsArray($aHoraires) Then ConsoleWrite('Horaires: ' & @CRLF) For $iJour = 0 To UBound($aHoraires) - 1 $vJourNom = StringRegExp($aHoraires[$iJour], '(?i)nom="(.*?)"', 3) If Not @error Then ConsoleWrite(' ' & $vJourNom[0] & @CRLF) $vOuverture = StringRegExp($aHoraires[$iJour], '(?i)ouverture="(.*?)"', 3) If Not @error Then ConsoleWrite(@TAB & 'Ouverture: ' & $vOuverture[0] & @CRLF) $vFermeture = StringRegExp($aHoraires[$iJour], '(?i)fermeture="(.*?)"', 3) If Not @error Then ConsoleWrite(@TAB & 'Fermeture: ' & $vFermeture[0] & @CRLF) Next EndIf ; Services $aServices = StringRegExp($aPDV[$Index], '(?is)<service>(.*?)<\/service>', 3) If IsArray($aServices) Then For $iService = 0 To UBound($aServices) - 1 ConsoleWrite('Service: ' & $aServices[$iService] & @CRLF) Next EndIf ; Prix $aPrix = StringRegExp($aPDV[$Index], '(?is)<prix(.*?)\/>', 3) If IsArray($aPrix) Then For $iPrix = 0 To UBound($aPrix) - 1 $vPrixNom = StringRegExp($aPrix[$iPrix], '(?i)nom="(.*?)"', 3) If Not @error Then ConsoleWrite('Prix: ' & $vPrixNom[0] & @CRLF) $vPrixID = StringRegExp($aPrix[$iPrix], '(?i)id="(.*?)"', 3) If Not @error Then ConsoleWrite(' ID: ' & $vPrixID[0] & @CRLF) $vPrixMaj = StringRegExp($aPrix[$iPrix], '(?i)maj="(.*?)"', 3) If Not @error Then ConsoleWrite(' Maj: ' & $vPrixMaj[0] & @CRLF) $vPrixValeur = StringRegExp($aPrix[$iPrix], '(?i)valeur="(.*?)"', 3) If Not @error Then ConsoleWrite(' Valeur: ' & $vPrixValeur[0] & @CRLF) Next EndIf ConsoleWrite(@CRLF) Next EndIf Edited April 16, 2024 by Andreik SOLVE-SMART 1
LoloSept Posted April 16, 2024 Author Posted April 16, 2024 Thanks Andreik! I understand better how 'Microsoft.XMLDOM' works. As for this 'regex' version, I will try to understand how it works for my personal culture.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now