Delphi with XML

xiaoxiao2021-03-06  60

Three methods for creating ixmldomDocument objects: (1) [Method 1] Create IXMLDOMDOCUMENT, Example (1) Uses msxml; var doc: ixmldomdocument; doc: = CoDOMDocument.create; ..... DOC: = nil; a) Doc.Load ('c: /TEMP.XML'); // From file load b) dynamically create VAR alement, reference2: ixmldomelement; // [anode: ixmldomnode ==> .appendchild ()]

// Add version information '' Doc.Appendchild (Doc.createProcessinginstruction ('XML', 'Version = "1.0" Encoding = "GB2312")); (*) because of this function The return result does not contain 'encoding = "GB2312", should pay attention before saving. // Add root knot doc.Appendchild (Doc.createElement (' bootdocnode ')); == alement // joins child node alement: = ixmldomelement (Doc.documentelement.Appendchild (Doc.createElement ('chilenode1')))); // Set contact attribute aElement.setttribute ('ID', '11'); reference.setttribute ('units', 'yuan / m2') ; // Set the node content alement.appendchild (Doc.createTextNode ('Node content')); // Subpoint Add sub-node alement2: = ixmldomelement (alement.Appendchild (Doc.createElement ('child_chilenode1')) );

(2) [Method 2] Create IXMLDocument directly (not ixmldomdocument) Uses XMLINTF, XMLDoc; var xmldoc: ixmldocument; anode: ixmlnode; s: String;

Xmldoc: = TXMLDocument.create (nil); TRY // Add version information '' xmldoc.active: = true; xmldoc.version: = '1.0'; xmldoc .Encoding: = 'GB2312'; // Add root node anode: = xmldoc.addchild ('bootdocnode'); // Add sub-node anode: = anode.addchild ('chilenode1'); // Set contact attribute anode .Setttribute ('ID', '22'); anode.setttribute ('units', 'yuan / m2'); // Set the node content anode.text: = 'node content'; // sub-node Add Sub node anode: = anode.addchild ('child_chilenode1'); anode.text: = 'child_chilenod content'; s: = xmldoc.xml.text; // .xml returned Tstrings Finally XmlDoc: = nil; end; (3) Using XMLDATABINDING I) Prepare XML files, this XML file has strong representation, guaranteeing the nodes used in the program, there is II), using File -> New -> XML Data Binding III) Create an XML object a) v: string; // XML file content Doc: IXMLBudgetDocTyp; // IXMLBudgetDocTyp Doc is the root of the XML file: = LoadXMLData (v) .GetDocBinding ( 'BudgetDoc', TXMLBudgetDocType) as IXMLBudgetDocType; B) Doc : = Loadingbudgetdoc ('c: /TEMP.XML'); IV) Apply Doc.childNodes.FindNode ('DOCFILE') as ixmldocfiletype;

(4) Use the TXMLDocument control XMLDocument1.FileName: = 'c: /TEMP.XML'; xmlDocument1.active: = true; // xmldocument1 equivalent to DOC

Other methods IXMLNodeList.FindNode (5) XML object (NodeName: DOMString): IXMLNode; IXMLNodeList.FindNode (NodeName, NamespaceURI: DOMString): IXMLNode; IXMLNodeList.FindNode (ChildNodeType: TGuid): IXMLNode; IXMLNodeList.FindSibling (const Node: IXMLNode; Delta: Integer): IXMLNode; IXMLNodeList.First: IXMLNode; IXMLNodeList.Last: IXMLNode; ... ... // Example (1) uses msxml; doc: IXMLDOMDocument; budgetdoc: ixmlDomNode; Rela: IxmlDOMNode; rs: IXmlDomnodeList; // Established or acquired XML node DOC: = CODOMDocument.create; Doc.Load ('c: /temp.xml'); budgetdoc: = doc.selectsinglenode ('budgetdoc'); Rela: = budgetdoc.selectsingLenode ( 'Rate');

// Create an XML sub-point if not assigned (trans (RELA); 'RELA.SetAttribute (' budgetid ', 0); Rela.setttribute (' Name ',' Name ') Budgetdoc.appendchild (transla);

// Take the child node (sequence) rs: = transla.selectNodes ('transla] "and @ Taskid =" 8 "]'); for i: = 0 to rs.length -1 do begin s : = S RS [i] .attributes.getnameditem ('newrate'). NodeValue; End; Rela: = Doc.childNodes.FindNode ('Domnode') as ixmldomnode;

// Remove sub-node for i: = rs.Length -1 Downto 1 Do Rela.removeChild (rs [i])

// Fi Father Node IF Assigned (Anode.ParentNode) and (anode.parentnode.NodeName = 'Task') Then Result: = anode.parentNode;

// Take the attribute docuMentelement.attributes ['name'] RS.NODES ['Name'] rsNodes [i] .attributes ['name']

AJOB: ixmldomnode; jobs: ixmldomnodelist; jobs: = ajob.selectnodes ('RCJ [@Attrib = "@Aattrib "]')

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

New Post(0)