0

I am trying to add a node a an XML file but can't figure it out. Any help would be highly appreciated Using VBA, I am trying to add a "<Poi />" under "<Song ..>" in my file database.xml - see expected result further down

I managed to create the node but it has the wrong formatting : spaces and carriage return are not added properly.

Here is an example :

Original database (before using any code) :

<?xml version="1.0" encoding="UTF-8"?>
<VirtualDJ_Database Version="8.4">
 <Song FilePath="\Music_Path\Author_1 - Title_1.mp3">
  <Tags Author="Author_1" Title="Title_1" Bpm="2.068965" Flag="1"/>
  <Infos SongLength="191.295000" FirstSeen="1584402618"/>
 </Song>
</VirtualDJ_Database>  

This database needs : 1 space before "<Song .." and 2 spaces before the others "<Tags ..", "<Infos..", "<Poi..."

Modified database (After processing the code) - Wrong result , see expected result further down

<?xml version="1.0" encoding="UTF-8"?><VirtualDJ_Database Version="8.4">
 <Song FilePath="\Music_Path\Author_1 - Title_1.mp3">
  <Tags Author="Author_1" Title="Title_1" Bpm="2.068965" Flag="1"/>
  <Infos SongLength="191.295000" FirstSeen="1584402618"/>
 <Poi/></Song>
</VirtualDJ_Database>

Expected result :

<?xml version="1.0" encoding="UTF-8"?>
<VirtualDJ_Database Version="8.4">  -------------------CARRIAGE RETURN BEFORE
 <Song FilePath="\Music_Path\Author_1 - Title_1.mp3">
  <Tags Author="Author_1" Title="Title_1" Bpm="2.068965" Flag="1"/>
  <Infos SongLength="191.295000" FirstSeen="1584402618"/>
  <Poi/> -----------(ADDED NODE----------------------- 2 SPACES BEFORE
 </Song> ----------------------------------------------  CARRIAGE RETURN + 1 SPACE BEFORE
</VirtualDJ_Database>

Code applied:

Sub ADD_NODE()

    Dim oXMLFileMod As MSXML2.DOMDocument60
    Set oXMLFileMod = New MSXML2.DOMDocument60
    oXMLFileMod.preserveWhiteSpace = True
    oXMLFileMod.Load "M:\VirtualDJ\database.xml"

    ------ (select a parent node)
Set ParentNode = oXMLFileMod.SelectSingleNode("/VirtualDJ_Database/Song[1]")

    ----- (add a new childNode)
Set childNode = oXMLFileMod.createElement("Poi")
ParentNode.appendChild (childNode)

oXMLFileMod.Save "M:\VirtualDJ\database.xml"

End Sub

How can I modify my code in order to have the proper formatting ?

  • 1
    XML doesn't care about spaces and carriage returns. If you need them preserved, look into `<![CDATA[...]]>` – Mathieu Guindon Mar 17 '20 at 14:42
  • If you want your XML "pretty printed" then take a look here: https://stackoverflow.com/questions/1118576/how-can-i-pretty-print-xml-source-using-vb6-and-msxml As @MathieuGuindon notes though, there's nothing "wrong" with what you have. XML is "human readable" but it's not really *for* reading. – Tim Williams Mar 17 '20 at 15:15

1 Answers1

0

For futur users, that how I did it : for spaces

Sub ADD_SPACE_BEFORE(CHEMIN_COMPLET_DB As String, SONG_NUMBER As Integer, ATTRIBUTE_NAME As String, ATTRIBUTE_NUMBER As Integer, NUMBER_OF_SPACES As Integer)

Dim xmlDoc As New MSXML2.DOMDocument60
Dim root As IXMLDOMElement
Dim MyText As IXMLDOMText
Dim MyNewNode As IXMLDOMNode
Dim SPACES As String
Dim i As Integer

xmlDoc.async = False
xmlDoc.preserveWhiteSpace = True
xmlDoc.Load "M:\VirtualDJ\database.xml"

   SPACES = ""
   For i = 0 To NUMBER_OF_SPACES - 1
        SPACES = SPACES & " "
   Next i

   Set root = xmlDoc.SelectSingleNode("/VirtualDJ_Database/Song[" & SONG_NUMBER & "]")
   Set MyText = xmlDoc.createTextNode(SPACES)
   Set MyNewNode = root.InsertBefore(MyText, root.SelectSingleNode("/VirtualDJ_Database/Song[" & SONG_NUMBER & "]/" & ATTRIBUTE_NAME & "[" & ATTRIBUTE_NUMBER & "]"))
   'MsgBox (xmlDoc.XML)

xmlDoc.Save CHEMIN_COMPLET_DB
End Sub

and for carriage return :

Sub ADD_CARRIAGE_RETURN(CHEMIN_COMPLET_DB As String, SONG_NUMBER As Integer, ATTRIBUTE_NAME As String, ATTRIBUTE_NUMBER As Integer, NUMBER_OF_RETURN As Integer, Optional EXTRA_SPACE As String)

Dim xmlDoc As New MSXML2.DOMDocument60
Dim root As IXMLDOMElement
Dim MyText As IXMLDOMText
Dim MyNewNode As IXMLDOMNode
Dim RETURNS As String
Dim i As Integer

xmlDoc.async = False
xmlDoc.preserveWhiteSpace = True
xmlDoc.Load "M:\VirtualDJ\database.xml"

   SPACES = ""
   For i = 0 To NUMBER_OF_RETURN - 1
        RETURNS = RETURNS & vbCrLf
   Next i
   If Not IsMissing(EXTRA_SPACE) Then RETURNS = RETURNS & " "


   Set root = xmlDoc.SelectSingleNode("/VirtualDJ_Database/Song[" & SONG_NUMBER & "]")
   Set MyText = xmlDoc.createTextNode(RETURNS)
   Set MyNewNode = root.InsertBefore(MyText, root.SelectSingleNode("/VirtualDJ_Database/Song[" & SONG_NUMBER & "]/" & ATTRIBUTE_NAME & "[" & ATTRIBUTE_NUMBER & "]"))

xmlDoc.Save CHEMIN_COMPLET_DB

End Sub