在VBScript中搜索正确的属性

时间:2019-01-10 15:34:51

标签: xml vbscript

我现在想要每个“ actie”的ID,并在循环中使用此ID号,以便我可以使用ID检查数据库中带有名称的名称。

但是我如何正确地在我的VBScript代码中获取我的ID号->属性,这样我就可以计算我的ID号,这样我就知道必须执行多少次循环,然后检查ID中的ID就可以了数据库。

我的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

2 个答案:

答案 0 :(得分:1)

您可以像这样遍历ID:

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

用适当的条件替换id_is_in_database

答案 1 :(得分:0)

以下代码段

vsnprintf

const auto buf_size = vsnprintf(nullptr, 0U, format, vlist) + 1; 返回为

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