Borland C ++ Builder 6.0 XML processing summary

Source: Internet
Author: User

Borland C ++ Builder 6.0 XML processing summary
1. Installation
C ++ builder encapsulates XML processing in the txmldocument component, which is part of the Internet component.
If the Borland Internet components component already exists in the installation package
Txmldocument control. The control is in the Internet control card, the author is in the last one, the icon is marked with XML
Document Style. If you do not have this control, you need to install it.
Txmldocument is not the default package installed by C ++ builder. You must add the installation package yourself. In C ++ builder
If the dclnet60.bpl file exists in the bin directory of the Directory, install it directly. Otherwise, you need to modify the installation to install
Package.

2. Use
2.1 basic operations
2.1.1 XML file loading using controls:
// Load the XML file <br/> opendialog1-> filter = "XML (Program configuration file) | *. xml"; <br/> If (! Opendialog1-> execute () {<br/> return; <br/>}</P> <p> xmldoc-> loadfromfile (opendialog1-> filename ); <br/> _ di_ixmlnode node = xmldoc-> documentelement; <br/> If (node = NULL) {<br/> showmessage ("is not a legal XML file format for the program configuration file. "); <Br/> return; <br/>}< br/>
2.1.2 example of saving an XML file using controls:

Savedialog1-> filename = fdevtypename + ". XML "; <br/> savedialog1-> filter =" XML (Program configuration file) | *. XML "; <br/> If (savedialog1-> execute () {<br/> xmldoc-> active = true; <br/> xmldoc-> Version = "1.0"; <br/> xmldoc-> encoding = "gb2312"; <br/> xmldoc-> options <donodeautoindent; <br/> // create the main element of the document <br/> xmldoc-> documentelement = xmldoc-> createelement ("Config ",""); <br/> xmldoc-> documentelement-> setattribute ("devtype", fdevtypename ); <br/> // update XML data from UI <br/> updatexmldatafromui (xmldoc-> documentelement ); <br/> // format XML <br/> xmldoc-> loadfromxml (formatxmldoc (xmldoc, 1 )); <br/> // Save the XML file <br/> xmldoc-> savetofile (savedialog1-> filename ); <br/> showmessage ("Save configuration" + savedialog1-> filename + "file succeeded"); <br/>}< br/>
2.2 read
2.2.1 read node data

Ansistring getnodedata (_ di_ixmlnode panode, <br/> ansistring nodename, ansistring defaultvalue) <br/>{< br/> // obtain the subnode list <br/> _ di_ixmlnodelist nodes = panode-> childnodes; <br/> // search nodes from the subnode list <br/> _ di_ixmlnode node = nodes-> findnode (nodename); <br/> If (node = NULL) {<br/> return defaultvalue; <br/>}< br/> // obtain data from the node <br/> return node-> gettext (); <br/>}
2.2.1 read node attributes
// Locate the column description in the format description XML file <br/> nodename = "Grid"; <br/> _ di_ixmlnode uinode = xmlui-> documentelement; <br/> _ di_ixmlnodelist uinodelst = uinode-> childnodes; <br/> uinode = uinodelst-> findnode (nodename); <br/> uinodelst = uinode-> childnodes; </P> <p> for (jdx = 0; jdx <uinodelst-> count; jdx ++) {<br/> // obtain the position of the current column in the table <br/> colname = uinodelst-> nodes [jdx]-> gettext (); <br/> // set the column header <br/> colheader = colname +" ("+ Uinodelst-> nodes [jdx]-> getattribute (" length "); </P> <p> // set the table column type based on the type, used to edit <br/> // obtain the node attribute example <br/> If (ansistring (uinodelst-> nodes [jdx]-> getattribute ("type ")) = ansistring ("hex") {<br/> colheader + = ", Hex "; <br/>} else if (ansistring (uinodelst-> nodes [jdx]-> getattribute ("type") = ansistring ("devtype ")) {<br/>} else if (ansistring (uinodelst-> nodes [jdx]-> getattribute ("type") = ansistring ("B Aud ") {<br/>} else if (ansistring (uinodelst-> nodes [jdx]-> getattribute (" type ") = ansistring (" integer ")) {<br/> colheader + = ", Dec"; <br/>}else {<br/> colheader + = ", Dec "; <br/>}< br/> colheader + = ")"; </P> <p> //... (Omitted) <br/> }//~ For (jdx... <br/>
2.3 write
The following code demonstrates that if you add a subnode and set attributes, the XML file is described in node 1st.
Void updatenodedata (_ di_ixmlnode panode, <br/> ansistring nodename, ansistring value, ansistring friendlyname) <br/>{< br/> _ inclunodes = panode-> childnodes; <br/> _ di_ixmlnode node = nodes-> findnode (nodename); <br/> If (node = NULL) {<br/> // Add a subnode <br/> node = panode-> addchild (nodename ); <br/> // set attributes <br/> node-> setattribute ("friendlyname", friendlyname ); <br/>}< br/> // change node data <br/> node-> settext (value); <br/>}
2.4 format XML
The above code is formatted XML, and the call method is described in Section 2.1.2 of XML writing. Use the indent method of this component directly
The formatted document cannot be obtained by writing to the node. When a file is loaded, it can be automatically formatted. A simple
The format is to save the file and read it again, and then save it to get the formatted document.

// Declaration: the code originates from the network, and made some modifications <br/> // formatted <br/> // format the XML document <br/> // formatted <br/> ansistring formatxmldoc (txmldocument * Doc, int indent) <br/>{</P> <p> ansistring SRES; <br/> int I; <br/> SRES = "<? XML version =/"" + doc-> Version + "/" encoding =/"" + doc-> encoding + "/"?> /R/N "; <br/> SRES + =" <"+ doc-> documentelement-> nodename; <br/> for (I = 0; I <doc-> documentelement-> attributenodes-> count; I ++) <br/> {<br/> SRES + = "" + doc-> documentelement-> attributenodes-> nodes [I]-> nodename <br/> + "= /" "+ doc-> documentelement-> attributenodes-> nodes [I]-> nodevalue + "/""; <br/>}< br/> SRES + = ">/R/N"; </P> <p> for (I = 0; I <doc-> documentelement-> childnodes-> count; ++ I) {<br /> SRES + = formatxmlnode (Doc-> documentelement-> childnodes-> nodes [I], indent ); <br/>}< br/> SRES + = "</" + doc-> documentelement-> nodename + ">/R/N"; <br/> return SRES; <br/>}</P> <p> ansistring formatxmlnode (_ di_ixmlnode element, int indent) <br/>{</P> <p> ansistring sblank = ""; <br/> ansistring SRES = ""; <br/> int I; <br/> for (I = 0; I <indent; ++ I) {<br/> sblank + = ""; <br/>}</P> <p> If (ELEM Ent-> nodetype = element_node <br/> & element-> childnodes-> count> 0 <br/> & element-> childnodes-> nodes [0]-> nodetype! = Text_node) <br/>{< br/> SRES = sblank + '<' + element-> nodename; <br/> for (I = 0; I <element-> attributenodes-> count; I ++) <br/> {<br/> SRES + = "" + element-> attributenodes-> nodes [I]-> nodename <br/> + "=/" "+ Element -> attributenodes-> nodes [I]-> nodevalue + "/""; <br/>}< br/> SRES + = ">/R/N"; <br/> indent ++; <br/> for (I = 0; I <element-> childnodes-> count; I ++) <br/> {<br/> SRES + = formatxmlno De (element-> childnodes-> nodes [I], indent ); <br/>}< br/> SRES + = sblank + "</" + element-> nodename + ">/R/N "; <br/>}< br/> else if (element-> nodetype! = Processing_instruction_node) <br/>{< br/> SRES + = sblank + element-> XML + "/R/N "; <br/>}< br/> return SRES; <br/>}</P> <p>

3. Notes
The method described above is to operate the XML document through the XML control. When using objects to operate XML files, pay attention to some issues.
When loading a file, you do not need to create a txmldocument object (txmldocument * myxml = new txmldocument ()),
Instead, use the _ di_ixmldocument interface. Loading an XML file from a file is an instance that uses loadxmldocument to obtain this interface. Operation
This interface is also used to obtain an empty XML document and start writing newxmldocument.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.