
xmlNewNode(name) :=
[
Local(lst);
lst := {};
lst[["name"]] := name;
lst[["props"]] := {};
lst[["content"]] := "";
lst[["childs"]] := {};
lst;
];

xmlSetName(node, name):=
[
 node[["name"]] := name;
];

xmlGetName(node) := node[["name"]];

xmlSetProp(node, prop, val):=
[
 node[["props"]][[prop]] := val;
];

xmlGetProp(node, prop):= node[["props"]][[prop]];


xmlSetContent(node, data):=
[
 node[["content"]] := data;
];

xmlGetContent(node):=  node[["content"]];


xmlInsertChild(node, child) :=
[
 DestructiveInsert( node[["childs"]], Length( node[["childs"]] )+1, child );
];


xmlGetChild(node, n) := node[["childs"]][[n]];

/*
xmlDocDumpFile(file, node) :=
[

 ToFile( file )
	[
         WriteString("<?xml version=\"1.0\"?>"); NewLine();
         WriteString("<");
         WriteString( xmlGetName(node) );
         {{x}, WriteString(" " : x[[1]] : "=\"" : x[[2]] : "\"")} /@  node[["props"]];
         WriteString(">");

        ];
];
*/


xmlTmpNodeToString(node) :=
[

 Local(tmp);

 /* Process name and properties */
 xmlTmpString := "<";
 xmlTmpString := xmlTmpString : xmlGetName(node);
 tmp :=  {{x}, " " : x[[1]] : "=\"" : x[[2]] : "\""} /@  node[["props"]];

 ForEach(i, tmp) xmlTmpString := xmlTmpString:i;

 xmlTmpString := xmlTmpString : ">";

 DestructiveInsert( xmlTmpFileString, 1, xmlTmpString );

  /* childs */
 ForEach(i, node[["childs"]]) xmlTmpNodeToString(i);

 xmlTmpString := xmlGetContent(node);
 If( xmlTmpString != "",
     DestructiveInsert( xmlTmpFileString, 1, xmlTmpString )
     );

 xmlTmpString := "</";
 xmlTmpString := xmlTmpString : xmlGetName(node);
 xmlTmpString := xmlTmpString : ">";

 DestructiveInsert( xmlTmpFileString, 1, xmlTmpString );

];



xmlDocDumpFile(file, node) :=
[
 xmlTmpFileString := {};
 xmlTmpString := "<?xml version=\"1.0\"?>";

 DestructiveInsert( xmlTmpFileString, 1, xmlTmpString );

 xmlTmpNodeToString(node);

 xmlTmpFileString := DestructiveReverse(xmlTmpFileString);

  ToFile( file )
    [
     ForEach(i,xmlTmpFileString) [WriteString(i); NewLine();];
    ];
];





