Delphi and XML example (directly using ixmldocument)

xiaoxiao2021-03-06  61

(*) 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 some

Begin

Nnode: = rootnode.childnodes.findsibling (cnode, 1); // ***** Find Next Number *****

Cnode: = nNode;

IF cnode <> nil damutes: = cnode.attributes ['proid'];

END;

END;

IF cnode = nil dam

did not find ****

Begin

CNODE: = rootnode.addchild ('product'); // ***** Add a node *****

Cnode.setttribute ('proproid', proid); // ***** Write point attribute ******

END;

Cnode.setttribute ('uninstall', 'false');

Nnode: = cnode.childnodes.findnode (nodename);

IF nnot = nil dam

Nnode: = cnode.addchild (nodename);

Nnode.setnodeValue (NodeValue); // ***** Writing 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 (proID <> ivalue) and (cnode <> nil) do // find some

Begin

Nnode: = rootnode.childNodes.Findsibling (CNODE, 1);

Cnode: = nNode;

IF cnode <> nil damutes: = 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-119056.html

New Post(0)