Accesso rapido:  

Forum: VirtualDJ Technical Support

Topic: Modify Database.xml - Special format

Questo argomento è obsoleto e potrebbe contenere informazioni obsolete o errate.

jem24hPRO InfinityMember since 2009
Hello,

I would like to add 3 POI automatically to everysong I own (the goal is to trigger the corresponding lightshow in Sunlite using OS2L)
In order to add the 3 POI, I figured I need to edit the Database.xml. I made a VBA macro to edit the file adding <Poi /> tags but as soon as I launch Virtual DJ, it tells me the database is corrupted and wants to repair it, then all lines I added are lost. What I add is 100% correct (I duplicate a POI generated by VDJ).
In fact, only opening and closing the Database.xml with VBA code generates the error popup (see example below, the library "microsoft XML 6.0" needs to be enabled)

Sub TEST_ERROR()
Set oXMLDoc = New MSXML2.DOMDocument60
oXMLDoc.LoadXML "M:\VirtualDJ\Database.xml"
oXMLDoc.Save "M:\VirtualDJ\Database.xml"
End Sub

I tried to edit manually with Atom and it works. (This is not a viable solution since I have several hundreds of songs to deal with)
I suspect 2 possible root-causes :
- A formatting when using VBA to save the file ?
- A check of the database size when VirtualDJ is open ?

My knowledge of XML handling is very basic. I'm desperate to find someone willing to test it and helping.
Thanks in advance




 

Inviato Tue 10 Mar 20 @ 10:35 am
locoDogPRO InfinityModeratorMember since 2013
I've done similar [different purpose] but I just used Ctrl+H with np++
find
</song>

replace with [this code should start with 1 space]
 <Poi Name="Sunlite1" Pos="15600.420" Type="action" Action=" " />\r\n  <Poi Name="Sunlite2" Pos="15600.420" Type="action" Action=" " />\r\n  <Poi Name="Sunlite3" Pos="15600.420" Type="action" Action=" " />\r\n </song> 

 

Inviato Tue 10 Mar 20 @ 10:46 am
VirtualDJ is a little picky when it comes to database.xml format.

The reason you're getting the corrupted error message when you open and save the database with VBA is because VBA removes all blank spaces (and under certain cases all CRLF chars as well)

VirtualDJ "demands" that <Song /> parent has one emtpy space in front of it, while all it's children have two empty spaces in front.

The last time I made an application (for my own purposes) in VB.NET that manipulated db data I had to "hack" database saving, by saving it as a plain text file, and then renaming it / changing it's extention to XML.
This way VB.NET would reserve the necessary empty spaces.
 

Inviato Tue 10 Mar 20 @ 11:21 am
jem24hPRO InfinityMember since 2009
Wow, I wish I had your skills PhantomDeejay
I posted this question on several VBA forums and didnt get the right answer. Now I understand what's going wrong.
Unfortunately, I'm not sure that I will be able to code the solution

Where did you perform your modifications ? (in the plain text file ?)
Thanks
 

Inviato Mon 16 Mar 20 @ 5:41 pm

Public VDJDB As XDocument
Public WorkingDBFilename As String ="D:\VirtualDJ\database.xml"

Private Sub bt_OpenDB_Click(sender As Object, e As EventArgs) Handles bt_OpenDB.Click
VDJDB = XDocument.Load(WorkingDBFilename)
End Sub


Private Function HackDBSave() As Boolean
Try
VDJDB.Save(WorkingDBFilename)
Dim DBReader As System.IO.StreamReader
Dim DBWriter As System.IO.StreamWriter
If My.Computer.FileSystem.FileExists(WorkingDBFilename & ".txt") Then
My.Computer.FileSystem.DeleteFile(WorkingDBFilename & ".txt", FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
End If
DBReader = New System.IO.StreamReader(WorkingDBFilename)
DBWriter = New System.IO.StreamWriter(WorkingDBFilename & ".txt")
DBWriter.WriteLine(DBReader.ReadLine)
Do Until (DBReader.Peek < 0)
Dim InputText As String
InputText = DBReader.ReadLine
If InputText.StartsWith(" <") Then
InputText = InputText.Substring(1)
ElseIf InputText.StartsWith(" <") Then
InputText = InputText.Substring(2)
End If
DBWriter.WriteLine(InputText)
Loop
DBReader.Close()
DBWriter.Close()
If My.Computer.FileSystem.FileExists(WorkingDBFilename) Then
My.Computer.FileSystem.DeleteFile(WorkingDBFilename, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
End If
My.Computer.FileSystem.RenameFile(WorkingDBFilename & ".txt", "database.xml")
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
Exit Function
End Try
Return True
End Function

That's my private Function (VB.NET) to save properly the database
 

Inviato Mon 16 Mar 20 @ 11:27 pm
jem24hPRO InfinityMember since 2009
Thanks for sharing the code! I wasn't expecting it.
In the end I won't be using it since I found how to maintain spaces in the XML database (the trick is : preserveWhiteSpace = True)
___________________________________________________________
Dim oXMLFileMod As MSXML2.DOMDocument60
Set oXMLFileMod = New MSXML2.DOMDocument60
oXMLFileMod.preserveWhiteSpace = True
oXMLFileMod.Load "M:\VirtualDJ\database.xml"

'Perform your XML action using https://excel-macro.tutorialhorizon.com/vba-excel-update-xml-file/

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

With this I managed to modify the existing Points of Interest (which is good already) but I didn't succeed to add one in the proper format
The 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 below
_________________________________
<?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()

'-------------BLACKOUT BEGINING
'Add a new node under existing 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
NUMBER_OF_POI = NUMBER_OF_POI + 1
Set childNode = oXMLFileMod.createElement("Poi")
ParentNode.appendChild (childNode)

'Add new Attribute to the Node
Set ParentNode = oXMLFileMod.SelectSingleNode("/VirtualDJ_Database/Song[1]/Poi[1]")

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

End Sub
_________________________________________________






 

Inviato Tue 17 Mar 20 @ 8:06 am
Yes, but when you preserve whitespace (which you should do anyway) you'll see that the number of spaces in front of each line doesn't match what VirtualDJ expects.
That's what the code above does. After saving the XML file (with preservewhitespace=true) it modifies the spaces in front of each line to match what VirtualDJ expects
 

Inviato Tue 17 Mar 20 @ 8:57 am


(Vecchi argomenti e forum sono automaticamente chiusi)