2

I'm adding child nodes to an existing node using VBScript. But the problem is that the output is something like this

<parentNode>
    <childNode/><addedChildNode/><anotherAddedChildNode/></parentNode>

And I need them to be like this:

<parentNode>
    <childNode/>
    <addedChildNode/>
    <anotherAddedChildNode/>
</parentNode>

I've tried to add a text node after each line that contains either vbCr, vbCrLf, vbLf, or vbNewLine like this

Set newLineNode= xmlDoc.createTextNode(vbCrLf)
parentNode.appendChild(newLineNode)  

But in any case, it gives me the following:

    <parentNode>
        <childNode/>
<addedChildNode/>
<anotherAddedChildNode/>
</parentNode>

What do you think should be done? And by the way the no of child nodes may reach around 400. And the file may be checked by other users. So format DOES matter.

5
  • 2
    That's not really a problem, for all practical matters the XML is the same with or without linebreaks. My recommendation would be not to spend too much time making it beautiful.
    – Tomalak
    Jul 30, 2015 at 17:22
  • @Tomalak I know this already, but the thing is the file is going to be checked by other users. And the added children may reach actually 400. So it would be almost impossible be read by users.
    – user5174680
    Jul 30, 2015 at 17:29
  • There should be some VBS pretty printing script around the Internet, you are very likely not the first person to have this problem. Have you searched?
    – Tomalak
    Jul 30, 2015 at 17:36
  • I think you might be able to massage a solution from this answer: stackoverflow.com/questions/1118576/…
    – Josie
    Jul 30, 2015 at 17:38
  • FTR I downvoted some of your previous questions, because they didn't show any research, were basically asking for someone to write code for you, and you couldn't be bothered to improve them when asked for clarification. Jul 30, 2015 at 17:47

4 Answers 4

2

If you are just trying to format the indentation and new lines, try this UD function.

Option Explicit

Function FormatXML(sXML)
    Dim sTmp ' Temp String
    Dim sOut ' Output string
    Dim lv ' level of indentation
    Dim oTmp ' Temporary object

    ' Remove newline characters, tabs
    sTmp = sXML
    sTmp = Replace(sTmp, vbCrLf, "")
    sTmp = Replace(sTmp, vbLf, "")
    sTmp = Replace(sTmp, vbCr, "")
    sTmp = Replace(sTmp, vbTab, "")

    ' Process contents split by the ending ">"
    sOut = ""
    lv = 0
    For Each oTmp In Split(sTmp, ">")
        oTmp = Trim(oTmp)
        If Len(oTmp) > 0 Then
            If Len(sOut) > 0 Then sOut = sOut & vbCrLf
            If Left(oTmp, 2) = "</" Then
                lv = lv - 1
                sOut = sOut & String(lv, vbTab) & oTmp & ">"
            Else
                If Left(oTmp, 1) = "<" And Right(oTmp, 1) <> "/" Then
                    sOut = sOut & String(lv, vbTab) & oTmp & ">"
                    lv = lv + 1
                Else
                    sOut = sOut & String(lv, vbTab) & oTmp & ">"
                End If
            End If
        End If
    Next
    FormatXML = sOut
End Function

Sample input:
" <parentNode> " & vbCrLf & vbTab & "<childNode/> <addedChildNode/><addedChildNode><GrandchildNode>ABC<addedGrandChildNode/></GrandChildNode></addedChildNode><anotherAddedChildNode/></parentNode>"

Sample output:
sample output

1
  • Thanks a lot @Patrick but I made a short solution.Many Thanks.
    – user5174680
    Jul 31, 2015 at 14:19
1

A quick search on Google revealed this blog post from Robert McMurray using an XSL transformation for pretty-printing XML:

xmlfile = "C:\path\to\your.xml"

Set xml = WScript.CreateObject("Msxml2.DOMDocument")
Set xsl = WScript.CreateObject("Msxml2.DOMDocument")

txt = Replace(fso.OpenTextFile(xmlfile).ReadAll, "><", ">" & vbCrLf & "<")
stylesheet = "<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">" & _
  "<xsl:output method=""xml"" indent=""yes""/>" & _
  "<xsl:template match=""/"">" & _
  "<xsl:copy-of select="".""/>" & _
  "</xsl:template>" & _
  "</xsl:stylesheet>"

xsl.loadXML stylesheet
xml.loadXML txt

If xml.parseError Then
  WScript.Echo xml.parseError.reason
  WScript.Quit 1
End If

xml.transformNode xsl

xml.Save xmlFile

This approach yielded better results to me than the SAXXMLReader/MXXMLWriter method outlined here, here, or here.

1
  • Thanks a lot @Ansgar Wiechers but I reached to a simple solution. You may check in bottom. Thanks again.
    – user5174680
    Jul 31, 2015 at 14:19
1

From the specs:

In editing XML documents, it is often convenient to use "white space" (spaces, tabs, and blank lines) to set apart the markup for greater readability. Such white space is typically not intended for inclusion in the delivered version of the document. On the other hand, "significant" white space that should be preserved in the delivered version is common, for example in poetry and source code.

As I told you before, you need to tell us why you what to include white space into the document. This you did in the comment you just posted:

@Tomalak I know this already, but the thing is the file is going to be checked by other users. And the added children may reach actually 400 childred. So it would be almost impossible be read by users.

Now reasonable answers are possible:

  1. Don't bother with pretty-print white space at all. Decent tools to view XML documents will ignore such white space and display the structure of the document correctly.
  2. Use a tool (e.g. tidy) or write a script/program to pretty-print the document; then it can be viewed in a dumb text editor.
  3. DON'T insert elements/nodes containing white space into the tree. That would change the structure (and thereby the meaning) of the document.
1
  • Thanks a lot @Ekkehard.Horner. I reached a short solution specific for my case. Thanks.
    – user5174680
    Jul 31, 2015 at 14:20
0

Thanks for all above answers but specifically for my case, I wrote the same two lines after each node and just added vbTab twice and it solved my problem.

Set newLineNode= xmlDoc.createTextNode(vbCrLf & vbTab & vbTab)
parentNode.appendChild(newLineNode)  
1
  • An atrocious violation of the spirit, concept, theory, and practice of XML. Jul 31, 2015 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.