1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
//------------------------------------------------------------------------------------------------------------------------------
// XMLWrite: Update the attribut to the xml document and create it if it does not exists
// example: XMLWrite(xml,'xml/Chambers/Chamber1/Name',Name);
//------------------------------------------------------------------------------------------------------------------------------
Procedure XMLWrite(XMLDoc: TXMLDocument; Path,Item, Value: String);
Var
S: string;
node: IXMLNode;
P: integer;
Begin
{$ifdef debug}
if Path[length(Path)]<>'/' then ShowMessage('Erreur le chemin doit se terminer par / dans la fonction XMLWrite dans common.pas:'+Path+' and item='+item);
if (Item='') or (Item[length(Item)]='/') or (Item[1]='/') then ShowMessage('Erreur dans la fonction XMLWrite, item non conforme: '+Item+' avec le path='+Path);
{$endif}
Path := lowercase(Path); // we change the path in small letters
P := Pos('/', Path);
S := Copy(Path, 1, P - 1);
delete(Path, 1, P);
node := XMLDoc.ChildNodes.FindNode(S); // Search the node
if (node = nil) and (S<>'') then node := XMLDoc.AddChild(S); // and create it if it doesn't exist
while (P > 0) do
Begin
P := Pos('/', Path);
if (P > 0) then
Begin
S := Copy(Path, 1, P - 1);
delete(Path, 1, P);
if (node.ChildNodes.FindNode(S) <> nil)
then node := node.ChildNodes.FindNode(S)
else node := node.AddChild(S);
End;
End;
if assigned(node) then node.Attributes[lowercase(item)] := Value; // node.Attributes[S] := Value;
End;
//------------------------------------------------------------------------------------------------------------------------------
// XMLRead : Read a value in a xml file
// example: CreationVersion := XMLRead(xml,'xml/version',version);
//------------------------------------------------------------------------------------------------------------------------------
Function XMLRead(XMLDoc: TXMLDocument; Chemin, default: String): String;
Var
S: string;
node: IXMLNode;
P: integer;
Begin
result := Default;
Chemin := lowercase(Chemin); // we change the path in small letters
P := Pos('/', Chemin);
S := Copy(Chemin, 1, P - 1);
delete(Chemin, 1, P);
node := XMLDoc.ChildNodes.FindNode(S);
while (node <> nil) and (P > 0) do
Begin
P := Pos('/', Chemin);
if (P > 0) then
Begin
S := Copy(Chemin, 1, P - 1);
delete(Chemin, 1, P);
End else S := Chemin;
if (P > 0) then // Si ce n'est pas le dernière identifiant, c'est un alors un noeud
Begin
node := node.ChildNodes.FindNode(S);
End else
Begin
if node.HasAttribute(S) then result := node.GetAttributeNS(S, '') else node := nil;
End;
End;
End; |
Partager