尝试在 VBScript 中添加节点 [英] Trying to add a node in VBScript

查看:19
本文介绍了尝试在 VBScript 中添加节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码将子节点 (HolderAccount) 重命名为 HolderAccount1 和 HolderAccount2.一些 HolderAccounts 可能有一个或两个 HolderAccount 子节点.我想新建一个HolderAccount,HolderAccounts里面只有一个HolderAccount.

I have some code that renames the child nodes (HolderAccount) to HolderAccount1 and HolderAccount2. Some HolderAccounts may have one or two HolderAccount child nodes. I want to create a new HolderAccount, where there is only one HolderAccount in HolderAccounts.

父节点 = HolderAccounts
子节点 = HolderAccount

Parent Node = HolderAccounts
Child Node = HolderAccount

输入样本:

<HolderAccounts>
    <HolderAccount>test</HolderAccount>        
</HolderAccounts>

<HolderAccounts>
    <HolderAccount>test</HolderAccount>
    <HolderAccount>test</HolderAccount>
</HolderAccounts>

我的输出:

<HolderAccounts>
    <HolderAccount1>test</HolderAccount1>
    <HolderAccount2/> 
</HolderAccounts>

<HolderAccounts>
    <HolderAccount1>test</HolderAccount1> 
    <HolderAccount2>test</HolderAccount2>
    <HolderAccount2/>       
</HolderAccounts>

我想要实现的目标:

<HolderAccounts>
    <HolderAccount1>test</HolderAccount1>
    <HolderAccount2></HolderAccount2> 
</HolderAccounts>

<HolderAccounts>
    <HolderAccount1>test</HolderAccount1> 
    <HolderAccount2>test</HolderAccount2>                      
</HolderAccounts>

Set xml = CreateObject("Microsoft.XMLDOM")
xml.async = False
count_var = 1

total_accounts = 0
total_modified_accounts = 0

'Get data from directory 
If xml.Load("c:\XML_DATA2.xml") Then
    'Find and give me a list of all HolderAccounts 
    For Each HolderAccounts In xml.SelectNodes("//HolderAccounts")
        'Find and give me a list of all HolderAccount    
        For Each HolderAccount In HolderAccounts.SelectNodes("./HolderAccount")
            'Check to see if you are pointing to 2nd HolderAccount in the HolderAccounts
            If count_var > 1 Then
                'Rename the 2nd HolderAccount in HolderAccounts
                Set accountEnum = xml.createNode(1, "HolderAccount" & count_var, "")

                'Give me the current nodes child nodes 
                For Each child In HolderAccount.childNodes
                    'Attach the child nodes to the account
                    accountEnum.appendChild(child.cloneNode(True))
                Next
                HolderAccounts.replaceChild accountEnum, HolderAccount

                total_modified_accounts = total_modified_accounts + 1
                xml.Save("c:\XML_DATA2.xml")
            Else    '1st HolderAccount 
                'Rename the 1st account 
                Set accountEnum = xml.createNode(1, "HolderAccount" & count_var, "")

                For Each child In HolderAccount.childNodes
                    accountEnum.appendChild(child.cloneNode(TRUE))
                Next
                HolderAccounts.replaceChild accountEnum, HolderAccount 

                'This is returning <HolderAccount2/> for nodes within <HolderAccounts>, 
                'I only want to create a new node where exist one node within <HolderAccounts>
                Set accountEnum2 = xml.createElement("HolderAccount2")
                HolderAccounts.appendChild(accountEnum2)

                xml.Save("c:\XML_DATA2.xml")
            End If
            count_var = count_var + 1
        Next           
        count_var = 1
        total_accounts = 0      
    Next
End If

Set node = Nothing
Set xml  = Nothing

推荐答案

我已经稍微简化了代码以更好地适应您的逻辑.看起来你已经改变了你的规范来枚举第一个节点,所以我们可以完全去掉 If 语句.您的 Else 块总是被调用,因为对于每个 HolderAccounts 容器,count_var 总是从 1 开始.

I've simplified the code a bit to better suit your logic. It looks like you've changed your specifications to enumerate the first node as well, so we can get rid of the If statement altogether. Your Else block was always being called since count_var always started at one for each HolderAccounts container.

相反,我们只想处理一个节点下只有一个账户的情况.最里面的循环将复制它,但我们还需要添加新的空 HolderAccount 节点.

Instead, we only want to handle the case where there is one account under a node. The inner most loop will copy it, but we need to add the new, empty HolderAccount node as well.

输入

<FabeDole>
    <HolderAccounts>
        <HolderAccount>test</HolderAccount>
    </HolderAccounts>
    <HolderAccounts>
        <HolderAccount>test</HolderAccount>
        <HolderAccount>test</HolderAccount>
    </HolderAccounts>
</FabeDole>

输出

<FabeDole>
    <HolderAccounts>
        <HolderAccount1>test</HolderAccount1>
        <HolderAccount2/>
    </HolderAccounts>
    <HolderAccounts>
        <HolderAccount1>test</HolderAccount1>
        <HolderAccount2>test</HolderAccount2>
    </HolderAccounts>
</FabeDole>

代码

Set xml = CreateObject("Microsoft.XMLDOM")
xml.async = False
count_var = 1

If xml.Load("C:\XML_DATA2.xml") Then
    For Each HolderAccounts In xml.SelectNodes("//HolderAccounts")
        Set HolderAccountCollection = HolderAccounts.SelectNodes("./HolderAccount")

        For Each HolderAccount In HolderAccountCollection

            Set accountEnum = xml.createNode(1, "HolderAccount" & count_var, "")
            For Each child In HolderAccount.childNodes
                accountEnum.appendChild(child.cloneNode(True))
            Next
            HolderAccounts.replaceChild accountEnum, HolderAccount

            count_var = count_var + 1
        Next

        If HolderAccountCollection.Length = 1 Then HolderAccounts.appendChild(xml.createNode(1,"HolderAccount" & count_var,""))
        count_var = 1   
    Next
    xml.Save("C:\XML_DATA2.xml")
End If

这篇关于尝试在 VBScript 中添加节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆