Delphi with XML

zhaozj2021-02-17  52

Three methods for creating ixmldomDocument objects: (1) [Method 1] Create IXMLDOMDOCUMENT?, Example (1) ?? Uses msxml; ?? var doc: = CoDOMDocument.create; ?? ... .. ?? Doc: = nil;? A) doc.load ('c: /TEMP.XML') ;? // From the file load? B) Dynamically create ?? var alement, willlement2: ixmldomenlement ;? // [anode: ixmldomnode ==>? .Appendchild ()]]

?? // Add version information '

'?? doc.Appendchild (' XML ',' Version = "1.0" encoding = "gb2312")); ???? (*) Because this function returns the result does not include 'encoding = "GB2312" 'Therefore, pay attention before saving. ?? // Add root node ?? doc.createElement (' bootdocnode ')); == aElement ?? // Add sub-node ?? alement: = ixmldomelement Doc.documentelement.appendchild ('chilenode1'))))); ?? // Set contact attribute ?? alement.setttribute ('ID', '11'); ?? alement.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 '; ??? // joined root nodes ??? 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'; ??? // Subpoint add sub-node ??? Anode: = anode.addchild ('child_chilenode1'); ??? anode.text: = 'child_chilenod content'; ?? ??? s: = xmldoc.xml.text;? //? .Xml returned to TSTRINGS? Finally ??? xmldoc: = nil;? End; (3) Using XMLDATABINDING ??? i) Prepare XML files, this XML file has strong representation, guarantee the program The nodes used and their relationships exist. "II) Create an XML object using File -> New -> XML Data Binding? III) ?? a) v: String; // XML file content ??? doc: ixmlbudgetDOCTYP // ixmlbudgetdoctyp is the root node of the XML file ??? doc: = loadingXMLData (V) .GetDocbinding ('budgetdoc' ) ;???) Application ??? doc.childNodes.FindNode ('docfile') as ixmldocfilety;

(4) Use the TXMLDocument control ??? xmldocument1.filename: = 'c: / temp.xml'; ??? xmldocument1.active: = true ;? // xmldocument1 equivalent to DOC

Other methods (5) XML objects ?? IXMLNodeList.FindNode (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; ?? ...? ??? // 例 (1) Uses msxml; ?? ??? doc: ixmldomdocument; ??? budgetdoc: ixmldomnode; ??? ixmldomnode; ??? xi ixmldomnodelist;

/ / Establish or get an XML node? Doc: = CodomDocument.create; Doc.Load ('c: /Temp.xml') ;? budgetdoc: = doc.selectsinglenode ('budgetdoc') ;? RELA: = BUDGETDOC. SelectSinglenode ('rate'); // Create an XML sub-node?? If not assigned (trans "begin ??? RELA: =? Doc.createElement ('rate'); ??? RELA.SetAttribute ('budgetID) ', 0); ??? RELA.SetAttribute (' name ',' name '); ??? budgetdoc.appendchild (relar);

// Take the child node (sequence)? Rs: = transla.selectnodes ('transla [@ budgetid = "2" 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 downnto 1 do ????? transla.removechild (RS [i])

// Fi Father Node ??? if Assigned (anode.parentnode) and (anode.parentnode.nodeename = 'task') THEN ????? Result: = anode.parentnode;

// Take the attribute? Doc.documentelement.attributes ['name']? Rela.attributes ['name']? Rs.nodes [i] .attributes ['name']

? Ajob: ixmldomnode; jobs: ixmldomnodelist ;? jobs: = ajob.selectnodes ('RCJ [@Attrib = "' aattrib '"]')

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

New Post(0)