Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5846165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:32:17+00:00 2026-05-22T12:32:17+00:00

What I am trying to do is creating a XML file by parsing a

  • 0

What I am trying to do is creating a XML file by parsing a XLS file.
An example should be more relevant:

| tag1      |           |           |           |
|           | tag2      |           |           |
|           |           | tag3      | tag3Value |
|           |           | tag4      | tag4Value |
|           | tag5      |           |           |
|           |           | tag6      | tag6Value |
|           |           |           |           |

If we imagine those are cells, will be equivalent for the following .xml code.

<tag1>
    <tag2>
        <tag3> tag3Value </tag3>
        <tag4> tag4Value </tag4>
    </tag2>
    <tag5>
        <tag6> tag6Value </tag6>
    </tag5>
</tag1>

That wouldn’t be so hard by managing one cell at a time and just doing “<” & Cell(x,y) & “>”
But I wanted an elegant solution. Here is my implementation so far:

Sub lol()
    Sheet1.Activate

    Dim xmlDoc As MSXML2.DOMDocument
    Dim xmlNode As MSXML2.IXMLDOMNode

    Set xmlDoc = New MSXML2.DOMDocument
    createXML xmlDoc
End Sub

Sub createXML(xmlDoc As MSXML2.DOMDocument)
    Dim newNode As MSXML2.IXMLDOMNode

    If Not (Cells(1, 1) = "") Then

        'newNode.nodeName = Cells(1, 1)
        ReplaceNodeName xmlDoc, newNode, Cells(1, 1)

        createXMLpart2 xmlDoc, newNode, 2, 2
        xmlDoc.appendChild newNode
    End If
    xmlDoc.Save "E:\saved_cdCatalog.xml"
End Sub

Sub createXMLpart2(xmlDoc As MSXML2.DOMDocument, node As MSXML2.IXMLDOMElement, i As Integer, j As Integer)
     Dim newNode As MSXML2.IXMLDOMElement
     If Not (Cells(i, j) = "") Then

        If (Cells(i, j + 1) = "") Then

            'newNode.nodeName = Cells(i, j)
            ReplaceNodeName xmlDoc, newNode, Cells(i, j)

            createXMLpart2 xmlDoc, newNode, i + 1, j + 1
        Else
            'newNode.nodeName = "#text"
            ReplaceNodeName xmlDoc, newNode, "#text"

            'newNode.nodeValue = Cells(i, j + 1)
            createXMLpart2 xmlDoc, newNode, i + 1, j
        End If
        node.appendChild (newNode)
    End If
End Sub

Private Sub ReplaceNodeName(oDoc As DOMDocument, oElement As IXMLDOMElement, newName As String)
        Dim ohElement As IXMLDOMElement
        Dim sElement As IXMLDOMElement
        Dim oChild As IXMLDOMNode

        ' search the children '
        If Not oElement Is Nothing Then
                Set ohElement = oElement.parentNode
                Set sElement = oDoc.createElement(newName)

                For Each oChild In oElement.childNodes
                        Call sElement.appendChild(oChild)
                Next

                Call ohElement.replaceChild(sElement, oElement)
        End If
End Sub

Problems: at first I didn’t realize that I can’t change the name of a node by doing node.nodeName = “newName”
I have found a solution on StackOverflow actually: Change NodeName of an XML tag element using MSXML

So i’ve commented my attempts at renaming the nodes and tried the version with the ReplaceNodeName method.

The actual problem: node.appendChild (newNode) from createXMLpart2 is giving me a problem: it sais that the variable “newNode” is no set.
I am puzzled.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:32:18+00:00Added an answer on May 22, 2026 at 12:32 pm

    Maybe something like this…

    Sub Tester()
    
    Dim r As Range
    Dim xmlDoc As New MSXML2.DOMDocument
    Dim xmlNodeP As MSXML2.IXMLDOMNode
    Dim xmlNodeTmp As MSXML2.IXMLDOMNode
    Dim bDone As Boolean
    
        Set r = ActiveSheet.Range("A1")
    
        Do While Not r Is Nothing
    
            Set xmlNodeTmp = xmlDoc.createElement(r.Value)
            If Len(r.Offset(0, 1).Value) > 0 Then
                xmlNodeTmp.appendChild xmlDoc.createTextNode(r.Offset(0, 1).Value)
            End If
    
            If Not xmlNodeP Is Nothing Then
                xmlNodeP.appendChild xmlNodeTmp
            Else
                xmlDoc.appendChild xmlNodeTmp
            End If
            Set xmlNodeP = xmlNodeTmp
    
            If Len(r.Offset(1, 0).Value) > 0 Then
                Set r = r.Offset(1, 0) 'sibling node
                Set xmlNodeP = xmlNodeP.ParentNode
            ElseIf Len(r.Offset(1, 1).Value) > 0 Then
                Set r = r.Offset(1, 1) 'child node
            Else
                Set r = r.Offset(1, 0)
                Set xmlNodeP = xmlNodeP.ParentNode
                Do While Len(r.Value) = 0
                    If r.Column > 1 Then
                        Set r = r.Offset(0, -1)
                        Set xmlNodeP = xmlNodeP.ParentNode
                    Else
                        Set r = Nothing
                        Exit Do
                    End If
                Loop
            End If
    
        Loop
        Debug.Print xmlDoc.XML
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataTable that I'm creating an XML file from using .WriteXML(..), although
I am trying to creating an optional association between a couple of tables. I
I'm trying to wrap my head around creating a toolbar (a tool band in
I am creating a chat using Ajax requests and I'm trying to get messages
I'm trying to choose a tool for creating UML diagrams of all flavours. Usability
I'm creating a CSS editor and am trying to create a regular expression that
I am trying to write an installer (by creating a .vdproj) that will work
I'm creating a threaded message board and I'm trying to keep it simple. There's
I am creating a reverse proxy and I am trying to find the optimal
I am considering creating my own website using Java and am trying to decide

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.