Fabe Dole
Fabe Dole

Reputation: 31

Trying to add a node in VBScript

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.

Parent Node = HolderAccounts
Child Node = HolderAccount

Input Sample:

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

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

My Output:

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

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

What I am trying to accomplish:

<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

Upvotes: 0

Views: 593

Answers (1)

user6836841
user6836841

Reputation:

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.

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.

Input

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

Output

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

Code

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

Upvotes: 1

Related Questions