我可以退回节点中的所有项目吗?

时间:2017-10-01 14:15:41

标签: xml vba

这是我的节点的样本

  <Validation>
    <Audit Node="Site" Count="1" />
    <Audit Node="Resident" Count="3" />
    <Audit Node="Lease" Count="3" />
    <Audit Node="Unit" Count="3" />
    <Audit Node="OpenCharges" Count="15" />
    <Audit Node="SureDeposit" Count="0" />
  </Validation>

以下代码段

Set xmlDoc = New DOMDocument60
If Not xmlDoc.LoadXML(indata) Then
    Err.Raise xmlDoc.parseError.ErrorCode, , xmlDoc.parseError.reason
End If
Set list = xmlDoc.SelectNodes("//Response/FileContents/Collections/Validation/Audit")
For Each node In list
    Set attr = node.Attributes.getNamedItem("Node")
    If (Not attr Is Nothing) Then
        Debug.Print attr.BaseName & " " & attr.Text
    End If
    If (node.HasChildNodes) Then
        For Each childNode In node.ChildNodes
            Debug.Print childNode.BaseName & " " & childNode.Text
        Next childNode
    End If
Next node

按预期收益

Node Site
Node Resident
Node Lease
Node Unit
Node OpenCharges
Node SureDeposit

有没有办法返回所有NamedItems?

所以我想要返回NODE,COUNT 我需要能够将命名项和相应的值保存到数组中供以后使用。这是我项目中较小的节点。我有几个,一个有50多个命名项目。

2 个答案:

答案 0 :(得分:0)

好像你在这里要求两件事。要回答如何返回所有节点:

//gaming Queue

#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
   int choice;
   string input;
   bool menu = true;  
   vector<string> favGames;
   while (menu){
       cout <<"Welcome to the favorite game queue please add your favorite games:\n";
       cout <<"1-Add a favorite game.\n";
       cout <<"2-List of your favorite games.\n";
       cout <<"3-Remove a game.\n";
       cin  >> choice;  
       switch (choice)
          {
             case 1:    
                cout << "Please add a favorite game to the queue: \n";
                string input;
                cin >> input;
                favGames.push_back(input);// Here is my problem it just jumps to case 2 and shows an error 
                break;
            case 2: 
                cout << "Here is a list of your favorite games.\n";
                break;

            default:
                cout << "You made an illegal choice.\n";
        }
    }
    return 0;
}

答案 1 :(得分:0)

Set xmlDoc = New DOMDocument60
If Not xmlDoc.LoadXML(indata) Then
    Err.Raise xmlDoc.parseError.ErrorCode, , xmlDoc.parseError.reason
End If
Set list = xmlDoc.SelectNodes("//Response/FileContents/Collections/Validation/Audit")
ReDim AuditN(list.Item(1).Attributes.Length)
ReDim AuditV(list.Length, list.Item(1).Attributes.Length)
For a = 0 To list.Item(1).Attributes.Length - 1
    SplString1 = Split(Mid(list.Item(1).XML, InStr(list.Item(1).XML, " "), Len(list.Item(1).XML)), "=", (list.Item(1).Attributes.Length * 2))(a)
    Debug.Print Split(SplString1, " ", 2)(1)
    AuditN(a) = Split(SplString1, " ", 2)(1)
Next a
nodeCounter = -1
For Each node In list
    nodeCounter = nodeCounter + 1
    For a = 0 To UBound(AuditN()) - 1
        Set attr = node.Attributes.getNamedItem(AuditN(a))

        If (Not attr Is Nothing) Then
            AuditV(nodeCounter, a) = attr.Text
        End If
        If (node.HasChildNodes) Then
            For Each childNode In node.ChildNodes
                Debug.Print childNode.BaseName & " " & childNode.Text
        Next childNode
        End If
    Next a
Next node

使用此解决方案,我有一个带有元素名称的数组,另一个带有我稍后可以使用的元素值。我怀疑有更清洁的方法吗?