0

I want to now the ID of each "actie" and use this ID number in a loop so I can check with the ID the Name with a Name in a Database.

But how do I get my ID number -> attribute I think, correctly in my VBScript code so I can count my ID numbers so I know how much loops I must do and then check of the ID is OK with the ID in the database.

My XML:

<?xml version="1.0"?>
<acties>
<actie ID = "1">
    <Name>Glen</Name>
    <Age>24</Age>
</actie>
<actie ID = "2">
    <Name>Jos</Name>
    <Age>32</Age>
</actie>
<actie ID = "3">
    <Name>Jasmien</Name>
    <Age>25</Age>
</actie>
</acties>

VBScript:

    Dim XMLo, XMLname, item, TextA, TextB,strQueryA, strQueryB, XMLage

    'Catch errors
    On Error Resume Next

    'load XML File
    Set XMLo = CreateObject("Microsoft.XMLDOM")
    XMLo.Async = "False"
    XMLo.Load("C:\Storage Card SD\map\TEST.xml")

    strQueryA =  "(/acties/actie/Name)"
    strQueryB = "(/acties/actie/Age)"

    Set elem = XMLo.DocumentElement.FirstChild

    Set XMLname= XMLo.SelectNodes(strQueryA)
    Set XMLage = XMLo.SelectNodes(strQueryB)

    For Each item In XMLname
        TextA = TextA & item.NodeName & ":" & " " & item.Text
    Next
    For Each item In XMLage
        TextB = TextB & item.Nodename & ":" & " "& item.Text
    Next

    SmartTags("Name") = TextA
    SmartTags("Age") = TextB

    If Err.Number <> 0 Then 
        ShowSystemAlarm "Error #" & CStr(Err.Number) & " " & Err.Description
        Err.Clear
        Exit Sub
    End If

    ShowSystemAlarm "Readout of XML Data Was Succesful!"
End Sub
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
G.VS
  • 1

2 Answers2

1

You can loop over the IDs like this:

For Each el In XMLo.SelectNodes("/acties/actie")
    id = el.GetAttribute("ID")
    '...
    'check ID against database
    '...
    If id_is_in_database Then
        name = el.SelectSingleNode("./Name").Text
        age  = el.SelectSingleNode("./Age").Text
        Exit For
    End If
Next

Replace id_is_in_database with a proper condition.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thanks for the replies! I want first to count all the ID's (attributes). If I have for e.g. 6 ID, I want then to loop over the ID's 6 times, and if the ID is not the same like in the database (DB) I want to send a message on the end of the loop "ID not found". If the ID is in the database, I want the loop to stop there and send me the and to my hmi via SmartTags use. I also have very fast the Error "Overloop"... Anybody now how I can become that? I have a very new to VBscript but I am already searching for 3 days now... Best regards – G.VS Jan 11 '19 at 08:36
  • @G.VS See updated answer. For further help please post a new question with your updated code. – Ansgar Wiechers Jan 11 '19 at 09:46
0

The following code snippet

strQueryC = "//acties"
Set XMLID  = XMLo.selectnodes(strQueryC)
For Each item In XMLID
  set IDs = Item.SelectNodes("actie")
  For Each xx In IDs
    TextC = TextC & vbNewLine & xx.GetAttribute("ID") & vbTab & xx.text
  Next
Next

returns TextC as

1       Glen 24
2       Jos 32
3       Jasmien 25
JosefZ
  • 28,460
  • 5
  • 44
  • 83