Add a node in a XML file
lazarus / 21-General
----------------------------------------------------------------------
folkeu08 · 2022-04-10 12:07
Hi
I create an xml file on the "OnCreate" event of a form :
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i 0
0 0 0
50 1 32
80
I am looking to add a "default" node between TPC100 and LowBand to
obtain this type of XML file :
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i 0
0 0 0
50 1
32 80
So is it possible to add a level?
I add my source which works for my first type of XML file.
Thanks
----------------------------------------------------------------------
AlexTP · 2022-04-10 15:08
Try to use this pkg:
https://wiki.freepascal.org/fcl-xml
----------------------------------------------------------------------
dseligo · 2022-04-10 16:00
You could do it in different way (maybe easier), but here it is
minimal change to your code that do what you want.
This is from 110 of Main_unit file:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i //*****
**********************************************************************
***************************** //******** Config
Low Band
************ //***************************************************
***************************************************** // Create a
parent node RootNode:= Doc.DocumentElement; parentNode :=
Doc.CreateElement('default'); // **** new line
RootNode.Appendchild(parentNode); // save
parent node **** moved line parentNode :=
Doc.CreateElement('LowBand');
RootNode.ChildNodes.Item[1].AppendChild(parentNode); // insert
child node in respective parent node **** new line // Create a
child node parentNode := Doc.CreateElement('Dens');
// create a child node //nofilho :=
Doc.CreateTextNode('S:\Database\cellule_archeologique.db'); //
insert a value to node nofilho := Doc.CreateTextNode('50');
parentNode.Appendchild(nofilho); // save node
RootNode.ChildNodes.Item[1].ChildNodes.Item[0].AppendChild(parentNod
e); // insert child node in respective parent node ****
changed parentNode := Doc.CreateElement('Gate'); //
create a child node nofilho := Doc.CreateTextNode('1'); //
insert a value to node parentNode.Appendchild(nofilho);
// save node RootNode.ChildNodes.Item[1].ChildNodes.Ite
m[0].AppendChild(parentNode); // insert child node in respective
parent node **** changed parentNode := Doc.CreateElement('AGC');
// create a child node nofilho :=
Doc.CreateTextNode('32'); // insert a value to node
parentNode.Appendchild(nofilho); // save node
RootNode.ChildNodes.Item[1].ChildNodes.Item[0].AppendChild(parentNod
e); // insert child node in respective parent node ****
changed parentNode := Doc.CreateElement('QPoint');
// create a child node nofilho := Doc.CreateTextNode('80');
// insert a value to node parentNode.Appendchild(nofilho);
// save node RootNode.ChildNodes.Item[1].ChildNodes
.Item[0].AppendChild(parentNode); // insert child node in
respective parent node **** changed
I marked with **** new and changed lines.
----------------------------------------------------------------------
folkeu08 · 2022-04-10 16:23
Hi
Thanks for your help, I test your solution after.
I find that now :
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i procedure
TCreateXML_Form.InitialiseXML; //Varialbles pour la création du
fichier config.XML var Doc: TXMLDocument; //
variable to document Racine, RootNode, Default, Funky, Jazz,
LowBand, MidBand, nofilho: TDOMNode; // variable to nodes begin
try // Create a document Doc := TXMLDocument.Create; //
Create a root node Racine := Doc.CreateElement('TPC100');
Doc.Appendchild(Racine); // save root
node //*********************************************************
*********************************************** //********
Config Low Band
************ //*************************************************
******************************************************* // Create a
parent node Default := Doc.DocumentElement; Default :=
Doc.CreateElement('Default'); Racine.Appendchild(Default);
// save parent node // Create a Child node
LowBand := Doc.DocumentElement; LowBand :=
Doc.CreateElement('LowBand'); Default.Appendchild(LowBand); //
Create a Sub Subchild node LowBand := Doc.CreateElement('Dens');
// create a child node nofilho :=
Doc.CreateTextNode('50'); LowBand.Appendchild(nofilho);
// save node
Default.ChildNodes.Item[0].AppendChild(LowBand); // insert child
node in respective parent node // Create a Sub Subchild node
LowBand := Doc.CreateElement('Gate'); // create a child
node nofilho := Doc.CreateTextNode('1'); // insert a value
to node LowBand.Appendchild(nofilho); //
save node Default.ChildNodes.Item[0].AppendChild(LowBand); //
insert child node in respective parent node LowBand :=
Doc.CreateElement('AGC'); // create a child node
nofilho := Doc.CreateTextNode('32'); // insert a value to
node LowBand.Appendchild(nofilho); // save
node Default.ChildNodes.Item[0].AppendChild(LowBand); //
insert child node in respective parent node LowBand :=
Doc.CreateElement('QPoint'); // create a child node
nofilho := Doc.CreateTextNode('80'); // insert a value to
node LowBand.Appendchild(nofilho); // save
node Default.ChildNodes.Item[0].AppendChild(LowBand); //
insert child node in respective parent node // Create a Child
node MidBand := Doc.DocumentElement; MidBand :=
Doc.CreateElement('MidBand'); Default.Appendchild(MidBand); //
Create a Sub Subchild node MidBand := Doc.CreateElement('Dens');
// create a child node nofilho :=
Doc.CreateTextNode('50'); MidBand.Appendchild(nofilho);
// save node
Default.ChildNodes.Item[1].AppendChild(MidBand); // insert child
node in respective parent node // Create a Sub Subchild node
MidBand := Doc.CreateElement('Gate'); // create a child
node nofilho := Doc.CreateTextNode('1'); // insert a value
to node MidBand.Appendchild(nofilho); //
save node Default.ChildNodes.Item[1].AppendChild(MidBand); //
insert child node in respective parent node MidBand :=
Doc.CreateElement('AGC'); // create a child node
nofilho := Doc.CreateTextNode('32'); // insert a value to
node MidBand.Appendchild(nofilho); // save
node Default.ChildNodes.Item[1].AppendChild(MidBand); //
insert child node in respective parent node MidBand :=
Doc.CreateElement('QPoint'); // create a child node
nofilho := Doc.CreateTextNode('80'); // insert a value to
node MidBand.Appendchild(nofilho); // save
node Default.ChildNodes.Item[1].AppendChild(MidBand); //
insert child node in respective parent node writeXMLFile(Doc,
'config.xml'); // write to XML finally
Doc.Free; // free memory
end;end;
It create what i want too.
Thanks
François
----------------------------------------------------------------------
folkeu08 · 2022-04-11 09:00
Hi,
I manage to create properly from the "dseligo" example of my XML file
with the 4 presets (default, rock, jazz, funky) for the values of
each band (low, mid, pres and hight).
The next step is to read the values of the 4 bands for the default
preset.
I was able to do this with this code without the default preset :
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i procedure T
Main_Form.ReadBaseDonneesXML;//https://wiki.freepascal.org/XML_Tutoria
l#Create_a_TXMLDocument_from_a_stringvar BaseNode: TDOMNode; Doc:
TXMLDocument; begin try // Read in xml file from disk
ReadXMLFile(Doc, 'config.xml'); // Retrieve the "LowBand" node
BaseNode := Doc.DocumentElement.FindNode('LowBand');
LowDens_Track.Value :=
StrToFloat(BaseNode.FindNode('Dens').TextContent);
LowGate_Track.Value :=
StrToFloat(BaseNode.FindNode('Gate').TextContent);
LowAGC_Track.Value :=
StrToFloat(BaseNode.FindNode('AGC').TextContent);
LowQPoint_Track.Value :=
StrToFloat(BaseNode.FindNode('QPoint').TextContent); // Retrieve
the "MidBand" node BaseNode :=
Doc.DocumentElement.FindNode('MidBand'); MidDens_Track.Value :=
StrToFloat(BaseNode.FindNode('Dens').TextContent);
MidGate_Track.Value :=
StrToFloat(BaseNode.FindNode('Gate').TextContent);
MidAGC_Track.Value :=
StrToFloat(BaseNode.FindNode('AGC').TextContent);
MidQPoint_Track.Value :=
StrToFloat(BaseNode.FindNode('QPoint').TextContent); // Retrieve
the "PresBand" node BaseNode :=
Doc.DocumentElement.FindNode('PresBand'); PresDens_Track.Value :=
StrToFloat(BaseNode.FindNode('Dens').TextContent);
PresGate_Track.Value :=
StrToFloat(BaseNode.FindNode('Gate').TextContent);
PresAGC_Track.Value :=
StrToFloat(BaseNode.FindNode('AGC').TextContent);
PresQPoint_Track.Value :=
StrToFloat(BaseNode.FindNode('QPoint').TextContent); // Retrieve
the "HighBand" node BaseNode :=
Doc.DocumentElement.FindNode('HighBand'); HighDens_Track.Value :=
StrToFloat(BaseNode.FindNode('Dens').TextContent);
HighGate_Track.Value :=
StrToFloat(BaseNode.FindNode('Gate').TextContent);
HighAGC_Track.Value :=
StrToFloat(BaseNode.FindNode('AGC').TextContent);
HighQPoint_Track.Value :=
StrToFloat(BaseNode.FindNode('QPoint').TextContent); finally //
finally, free the document Doc.Free; //ReadLn; end;end;
I try to introduce the item as in the creation but it does not read
anything in fact.
I'm lost again!
Thanks for your new help.
I added as an attachment, the file that is created
François