Using XML in C ++ Builder6

zhaozj2021-02-16  52

Recently, someone often saw someone inquiry how to use XML in C Builer, starting the author also thinking is inexplicable, first XML technology seems to be linked to the network, or SOAP / Web Service, second C Builer provides TXMLDocument components, it should be very user-friendly. These days were studying Tomcat, found that its profile was written in XML files, and it seems that this represents the software's advanced type :), so I also decided to use C Builer to operate XML files to increase their own software. At this time, I deeply feel the confusion of those inquiors, and the TXmldocument component is really difficult. If there is an experience of the ASP XML construction website, I really have to be difficult to fall by this problem :), nonsense, talk to the topic .

Processing XML files must of course require XML Parser, and XML Parse includes SAX, DOM, and SAX is relatively simple. The DOM model is more difficult, and C Builer is using the DOM model to process XML files, so mainly discussed here DOM model processing method.

The TXMLDocument component uses the MS XML DOM model to process XML, MSXmldom is COM based, using this COM in tools such as VB, and it is not easy in C Builer, I think it is large because We are very easy to understand and use, and many methods are very easy to understand. Many methods are meaningless, and even help do not need to be checked, and the author has to check when using TXMLDocument, many methods can not be accurate Know what it means, and even traps.

Below I told everyone how to use TXMLDocument to process XML. This example is that, the author needs a minor program for a timed reminder schedule, assuming that other functions are completed, how is the problem now? Save our schedule, here in order to use XML, it is of course stored in XML file format. For a schedule, it is definitely necessary to add a deleted function, and the substantially above operation already contains all of the processing XML.

Let's design XML file (TIP.XML): // Note The encoding format here is GB2312, otherwise there is garbled 0

The first is to open XML, the current code snippet shows this process: xmldoc-> loadFromfile (WideString (AppPath) "Tip.xml"))); // Xmldoc is the name of TXmldocument xmlDoc-> Active = true; FillList (); // Add XML's content to listView then read the contents of XML (FillList):

lstMain-> Items-> BeginUpdate (); lstMain-> Items-> Clear (); _di_IXMLNode node = XMLDoc-> DocumentElement; // DocumentElement property is used to obtain the root node _di_IXMLNodeList nodes = node-> ChildNodes; // child node to give List for (int N = 0; n count; n ) {tlistItem * Li = LSTMAIN-> items-> add (); node = nodes-> nodes [n] -> childnodes-> nodes [0] ; // Get the child node of the child node, that is, vtip // tip // mode int m = string (node-> getText ()). Toint (); // get the text content of the corresponding node and convert to int Li -> CAPTION = ""; Li-> ImageIndex = m 5; Li-> Subitems-> add (nodes-> nodes [n] -> childnodes-> nodes [1] -> getText ()); li-> Subitems-> add (nodes-> nodes [n] -> childnodes-> nodes [2] -> getText ()); // code is the same} LSTMAIN-> items-> endupdate (); after the above code, let's see To the results similar to the following figure:

Adding a node element: bool __fastcall Tfrmmain :: AddTask (int m, TDateTime & date, TDateTime & time, String con) {// TODO: Add your source code here _di_IXMLNode node = XMLDoc-> DocumentElement; node = node-> AddChild (WideString ("Tip")); _di_ixmlnode subnode = node-> addchild (WideString ("M))); Subnode-> Settext (WideString (M)); Subnode = Node-> AddChild (" Time "); Subnode -> setText (Date.Datestring () "?" Time.TimeString ())); Subnode = Node-> AddChild (WideString ("Content")); Subnode-> Settext (WideString (CON)); Return True;} // The above code is easy to understand, you can control the format of the XML file when reading

Finally, delete: int indected-> index; // indi_ixmlnode node = xmldoc-> documentElement; int REETE-> CHILDES-> delete (ind); if (Re! = -1) Lstmain-> items-> delete (ind); the above code demonstrates how to handle an XML file, the reader can look at the help you can know how to handle more complex XML file formats.

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

New Post(0)