C ++ builder uses XMLDocument to add, delete, modify, and read XML!

Source: Internet
Author: User

// Header file

# Include <XMLDoc. hpp>

# Include <xmldom. hpp>

# Include <XMLIntf. hpp>

 

// Operation

// Define variables

AnsiString GameTDJpath; // It can only be of the AnsiString type

AnsiString gamepath = "gamepath"; // only the AnsiString type is allowed

_ Di_IXMLDocument XmlNetbargpp = NewXMLDocument (); // create a new object

XmlNetbargpp-> Active = true; // activate

 

// Read

XmlNetbargpp-> LoadFromFile (ExtractFilePath (Application-> ExeName) + "netbar. xml"); // load xml (the dynamic path is obtained by combining the exe path and XML name)

XmlNetbargpp-> Active = false; // manually disable and enable it for multiple times

XmlNetbargpp-> Active = true; // manually disable and enable it for multiple times

GameTDJpathtext = XmlNetbargpp-> DocumentElement-> ChildNodes-> FindNode (gamepath)-> GetAttribute ("path"); // you can find the value of the path attribute of the node gametdj.

 

// Insert

_ Di_IXMLNode root = XmlNetbargpp-> DocumentElement; // obtain the ROOT node root.

Root = root-> AddChild ("game"); // Add a node

Root-> Attributes ["path"] = ""; // Add the attribute path value as null.

XmlNetbargpp-> SaveToFile ("netbar. xml"); // update XML only after saving

 

// Update

_ Di_IXMLNode nodeupdate = XmlNetbargpp-> DocumentElement-> ChildNodes-> FindNode (gamepath); // search for the gamepath Node

Nodeupdate-> AttributeNodes-> operator [] (0)-> NodeValue = (WideString) ("path value "); // set the path value of the first attribute under the node found by node.

XmlNetbargpp-> SaveToFile ("netbar. xml"); // update xml only after saving

 

// Delete

_ Di_IXMLNode node2 = XmlNetbargpp-> DocumentElement; // you can search for it and then delete it.

Node2-> ChildNodes-> Delete (WideString ("gamepath"); // Delete a node

GamepathXmlNetbargpp-> SaveToFile ("netbar. xml"); // The XML is updated only after being saved.

 

<Pre>

 

 

From an empty column

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.