Delphi has been working with XML since long ago, the latest version contains three kinds of XML interpreter, one is MSXML, look at the name to know
This is Microsoft's own, according to Delphi official said is the speed is the fastest, but can only be used on Windows. The other two are
Omnixml, ADOM, these two are cross-platform. But the speed is slower than MSXML. Inside Delphi 10.2, WINDOWS
Apps use MSXML by default, and Linux apps use Omnixml by default. Of course, you can change it by force.
Today we'll talk about how to work with XML using the KBMMW built-in XML interpreter. Why use the KBMMW XML interpreter?
The main reason is the cross-platform, while the author claims that it is fast (I did not make a specific comparison), and most importantly it can be associated with Json,yaml,bson
Convert directly to each other.
First of all, in KBMMW XML, there are mainly two objects, one is Tkbmmwdomxml, he represents the entire XML object, in addition
One is Tkbmmwdomxmlnode, which represents the nodes in the XML.
We built the simplest XML object.
Procedure Tform2.button1click (sender:tobject); var dom:tkbmmwdomxml; Node:tkbmmwdomxmlnode;begin Memo1. Lines.clear; Dom:=tkbmmwdomxml.create; Try dom. Preservewhitespace:=true; Dom. Autolinefeed:=true; Node:=tkbmmwdomxmlnode.create (DOM); Node. name:= ' xml '; Node. Isdeclaration:=true; Node. attribbyname[' version ']:= ' 1.0 '; Node. attribbyname[' encoding ']:= ' UTF-8 '; Node. attribbyname[' standalone ']:= ' no '; Dom. Root.Nodes.AddNode (node); Memo1. Lines.add (DOM. Savetostring (nil)); Finally Dom. Free; End;end;
The results of the operation are as follows:
<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?>
We can continue to join a node.
Procedure Tform2.button1click (sender:tobject); var dom:tkbmmwdomxml; Node:tkbmmwdomxmlnode;begin Memo1. Lines.clear; Dom:=tkbmmwdomxml.create; Try dom. Preservewhitespace:=true; Dom. Autolinefeed:=true; Node:=tkbmmwdomxmlnode.create (DOM); Node. name:= ' xml '; Node. Isdeclaration:=true; Node. attribbyname[' version ']:= ' 1.0 '; Node. attribbyname[' encoding ']:= ' UTF-8 '; Node. attribbyname[' standalone ']:= ' no '; Dom. Root.Nodes.AddNode (node); Node:=tkbmmwdomxmlnode.create (DOM); Node. name:= ' creator '; Node. data:= ' xalion '; Dom.root.Nodes.AddNode (node); Memo1. Lines.add (DOM. Savetostring (nil)); Finally Dom. Free; End;end;
The results of the operation are as follows:
<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?><creator>xalion</creator>
Continue inserting a set of data
Procedure Tform2.button1click (sender:tobject); var dom:tkbmmwdomxml; Node,datanode,rownode:tkbmmwdomxmlnode; I:integer;begin Memo1. Lines.clear; Dom:=tkbmmwdomxml.create; Try Dom. Preservewhitespace:=true; Dom. Autolinefeed:=true; Node:=tkbmmwdomxmlnode.create (DOM); Node. name:= ' xml '; Node. Isdeclaration:=true; Node. attribbyname[' version ']:= ' 1.0 '; Node. attribbyname[' encoding ']:= ' UTF-8 '; Node. attribbyname[' standalone ']:= ' no '; Dom. Root.Nodes.AddNode (node); Node:=tkbmmwdomxmlnode.create (DOM); Node. name:= ' creator '; Node. data:= ' xalion '; Dom.root.Nodes.AddNode (node); Datanode:=tkbmmwdomxmlnode.create (DOM); Datanode. name:= ' result '; Dom. Root.Nodes.AddNode (Datanode); For I: = 1 to ten do begin rownode:=tkbmmwdomxmlnode.create (DOM); Rownode. name:= ' Row '; Node:=tkbmmwdomxmlnode.create (DOM); Node.name:= 'ID '; Node. Setasint (i); Rownode. Nodes.addnode (node); Node:=tkbmmwdomxmlnode.create (DOM); node.name:= ' name '; Node. data:= ' xalion '; Rownode. Nodes.addnode (node); Node:=tkbmmwdomxmlnode.create (DOM); node.name:= ' Date '; Node. Setasdatetime (now); Rownode. Nodes.addnode (node); Datanode. Nodes.addnode (Rownode); End Memo1. Lines.add (DOM. Savetostring (nil)); Finally Dom. Free; End;end;
The output results are as follows:
<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?><creator>xalion</creator><result> <row><id xsd:type= "int" >1</id><name>xalion</name><date xsd:type= "datetime" > 2017-07-24t01:37:51.761+08:00</date></row><row><id xsd:type= "int" >2</id><name >xalion</name><date xsd:type= "datetime" >2017-07-24T01:37:51.761+08:00</date></row> <row><id xsd:type= "int" >3</id><name>xalion</name><date xsd:type= "datetime" > 2017-07-24t01:37:51.761+08:00</date></row><row><id xsd:type= "int" >4</id><name >xalion</name><date xsd:type= "datetime" >2017-07-24T01:37:51.761+08:00</date></row> <row><id xsd:type= "int" >5</id><name>xalion</name><date xsd:type= "datetime" > 2017-07-24t01:37:51.761+08:00</date></row><row><id xsd:type= "int" >6</id><name >xalIon</name><date xsd:type= "datetime" >2017-07-24t01:37:51.761+08:00</date></row><row ><id xsd:type= "int" >7</id><name>xalion</name><date xsd:type= "datetime" > 2017-07-24t01:37:51.761+08:00</date></row><row><id xsd:type= "int" >8</id><name >xalion</name><date xsd:type= "datetime" >2017-07-24T01:37:51.761+08:00</date></row> <row><id xsd:type= "int" >9</id><name>xalion</name><date xsd:type= "datetime" > 2017-07-24t01:37:51.761+08:00</date></row><row><id xsd:type= "int" >10</id><name >xalion</name><date xsd:type= "datetime" >2017-07-24T01:37:51.761+08:00</date></row> </result>
The above describes the creation of XML in KBMMW, so how to access these nodes?
The simplest way is to traverse and find one, but this method is too cumbersome. In KBMMW, you can use the path to
Access the node.
Procedure Tform2.button2click (sender:tobject); var dom:tkbmmwdomxml; Node,datanode,rownode:tkbmmwdomxmlnode;begin Memo2. Lines.clear; Dom:=tkbmmwdomxml.create; Try dom. Loadfromstring (Memo1. Lines.text); Node:=dom.getnode ('//creator '); If Node<>nil then Memo2. Lines.add (node. Data) finally Dom. Free end;end;
Run results
Keep it a little more complicated.
Procedure Tform2.button2click (sender:tobject); var dom:tkbmmwdomxml; Node,datanode,rownode:tkbmmwdomxmlnode; I:integer; J:integer;begin Memo2. Lines.clear; Dom:=tkbmmwdomxml.create; Try dom. Loadfromstring (Memo1. Lines.text); Datanode:=dom.getnode ('//result '); If Datanode<>nil then begin if Datanode. Nodes.count>0 then begin for I: = 0 to Datanode. Nodes.count-1 do begin Rownode:=datanode. Nodes[i]; For J: = 0 to Rownode. Nodes.count-1 do begin Node:=rownode. NODES[J]; If Node<>nil then Memo2. Lines.add (node. Data); End; End; End; End; Finally Dom. Free end;end;
Run results
That's basically it.
Http://www.cnblogs.com/xalion/p/7225682.html
Getting started with XML operations in KBMMW (cross-platform, and can be directly converted to Json,yaml,bson)