Delphi and XML example (directly using ixmldocument)

xiaoxiao2021-03-06  70

(*) Uses MSXML, XMLINTF, XMLDOC;

(1) // from file access function writeXML (XMLName, nodeName, nodeValue: string; proID: integer): integer; var xmlDoc: IXMLDocument; rootNode, CNode, NNode: IXMLNode; iValue, iReturn: integer; sValue: string; Begin Result: = -1; if not fileexists (xmlname) THEN EXIT; XMLDoc: = txmldocument.create (nil); // ***** Create TXMLDocument ***** xmldoc.active: = true; IReturn: = -1; Try XmlDoc.LoadFromFile (XMLNAME); // ***** Load XML file ***** xmldoc.version: = '1.0'; xmldoc.encoding: = 'gb2312'; rootnode: = xmldoc .Childnodes.findNode ('Document'); // ***** Find Nose ***** CNode: = rootnode.childNodes.FindNode ('Product'); if cnode <> nil dam // iValue: = Cnode.getnodeValue; // ***** Take no point value ***** ivalue: = cnode.attributes ['proid']; // ***** Nord point attribute ***** While (Proid <> ivalue) and (cnode <> nil) do // find a begin nNode: = rootnode.childnodes.findsibling (C Node, 1); // ***** Find the next node ***** cnode: = nNode; if cnode <> nil damutes: = cnode.attributes ['proid']; end; End; if Cnode = nil dam // I didn't find **** begin cnode: = rootnode.addchild ('product'); // ***** Add a node ***** cnode.setttribute ('proid ", ProID); // ***** Write point attribute ***** * End; cnode.setttribute ('uninstall', 'false'); nNode: = cnode.childNodes.findNode (nodename); if nnode = nil The nnode: = cnode.addchild (nodename); nnote.setnodeValue (NodeValue);

// ***** Write point value ***** xmldoc.savetofile (XMLName); Except IReturn: = - 1; end; finally xmldoc: = nil; // ***** Release TXMLDocument *** ** result: = iReturn; end; end; (2) deleting the node function removeNode (XMLName: string; proID: integer): integer; var xmlDoc: IXMLDocument; rootNode, CNode, NNode: IXMLNode; iValue, iReturn: integer; sValue: string; begin result: = -1; if not fileExists (XMLName) then exit; xmlDoc: = TXMLDocument.Create (nil); xmlDoc.Active: = True; iReturn: = - 1; try try xmlDoc.LoadFromFile (XMLName ); xmlDoc.Encoding: = 'GB2312'; rootNode: = xmlDoc.ChildNodes.FindNode ( 'Document'); CNode: = rootNode.ChildNodes.FindNode ( 'product'); if CNode <> nil then begin iValue: = CNode .Attributes ['proid']; while (cnode <> nil) DO / / find a begin nNode: = rootnode.childNodes.Findsibling (cnode, 1); cnode: = nNode; if cnode < > NIL THEN IVALUE: = cnode.attributes [ 'ProID']; END;

End; if cnode <> nil damnode.childnodes.remove (cnode); // Find, delete

XMLDoc.savetofile (XMLName); IReturn: = 0; Except IReturn: = - 1; End; Finally Xmldoc: = nil; result: = IRETURN; END;

转载请注明原文地址:https://www.9cbs.com/read-121189.html

New Post(0)