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 ?