/ * This is an XML file class I wrote, encapsulated ixmldocument, used to establish, read and write XML files in the C Builder program, and it feels more than a year of modification and use.
Author: lao-hong * /// Here's what "my_xml.h" of: (uses STL) // ------------------------ -------------------------------------------------- - // 2003-1-26
#ifndef my_xmlh # Define my_xmlh
#include
#include
Void save_to (ANSISTRING FPATH_) const; // Save to another file}; // ------------------------------- ------------------------------------------ TypedEf Map
Ansistring Child_Value (ANSISSTRING NAME) const; // Returns the value of one child node of the node. Name is a node name of the child node.
Void Set_Value (Ansistring Value_) const; // Set the value of this node.
Void Set_Attribute (ANSISSTRING NAME, ANSISTRING VALUE) const; // Sets the value of this node. Shape: name = "value"
Void Set_Child_Value (ANSISSTRING NAME, ANSISTRING VALUE) const; // Sets the value of a child node of this node. Shape:
XMLNode Find_Child_Node (ANSISTRING NAME) const; // Returns the name is the child node of Name, if any.
XMLNode get_child_node (ANSISUSTRING NAME) const; // Returns a name is the child node of Name, if not, create one.
Void get_attrs (MAP
Void set_attrs (const map Void find_child_nodes (Vector #include "my_xml.h" / / -------------------------------------------------------------------------------------------- --------------------------- # prgma package (smart_init) // --------------- -------------------------------------------------- --------- // --------------------------------------- -------------------------------- MyXML :: MyXML (): Xmldoc (null), root (null), Savepause (0) {} // ------------------------------------------- ---------------------------- MyXML :: MyXML (ANSISTRING FPATH): XMLDoc (null), root (null), Savepause (0 ) {INIT (FPATH);} // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------- void myxml :: init (ansistring fpath) {_fpath = fpath; if (null! = Xmldoc) delete Xmldoc; // I have no confidence in this line of code, please also ask the high guidance. Xmldoc = null; if (fpath.length () == 0) {} else {if (fileexists (fPath)) // If the file exists {Try {xmldoc = loadingXmldocument (fpath);} catch (...) {xmldoc = NULL;}} Else // If the file does not exist, {Try {XMLDoc = NewXMLDocument (l "1.0");} // creation, L "" means a wide string. Catch (...) {xmldoc = NULL;} f (null == xmldoc) {} else xmldoc-> filename = fpath;}}}} (xmldoc == null) {root = null;} else {xmldoc-> encoding = Ansistring ("UTF-16"); // The encoding method can also be selected: // "GB2312" and the general Chinese text compatible // "UTF-8" is suitable for all English. // "BIG5" Taiwan Traditional Chinese Root = xmldoc-> documentelement; if (root == null) {xmldoc-> addchild ("root"); // root node, there must be one. Generally use "root" root = xmldoc-> documentelement;}} SavePause = 0;} // -------------------------------------------------------------------------- ---------------------------------------- BOOL MYXML :: ISNULL () Const {Return (Root == null);} // ---------------------------------------- ----------------------------- void myxml :: save () const // store {if (root == null) Return; IF (SavePAUSE> 0) Return; if (_fpath.length () == 0) Return; if (xmldoc-> modified {Try {xmldoc-> savetofile (_fpath);} catch (...)}}}} / / -------------------------------------------------------------------------------------------- --------------------- xmlnode myxml :: root () const {return xmlnode (root);} // ----------- -------------------------------------------------- -------- Void myxml :: disable_save () { savepause;} // ------------------------- ----------------------------------------- void myXML :: enable_save () { --Savepause; Save ();} // --------------------------------------- ------------------------------ Void myXML :: Save_to (ANSISTRING FPATH_) Const / / Save to ... { IF (root == null) return; try {xmldoc-> savetofile (fpath_);} catch (...) {}} // ------------------- -------------------------------------------------- / / =========================================================================================================================================================================================== =========================== / / -------------------------------------------------------------------------------------------- ------------------- // This function is used to replace the illegal characters contained in the string to prevent it from writing into an XML file. // This simply replaces it, it is not necessarily suitable. Say everyone to correct. Static Ansistring & Valid (ANSISTRING & STR) {const INT LEN = str.length (); for (int i = 1; i <= le; i) {char & c = str [i]; if (c> 0x00 && c <0x20) c = '';} Return Str;} / / -------------------------------------------------------------------------------------------- ----------------------- xmlnode :: xmlnode () {_node = null;} // ------------- -------------------------------------------------- -------- xmlnode :: xmlnode (_di_ixmlnode node) {_node = node;} // ------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------- Void XMLNode :: init (_di_ixmlnode node) {_node = node;} // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------- Bool XMLnode :: isnull () const {return (_node == null); } // ----------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ XMLNode & XMLnode :: Operator = (const Xmlnode & node_) {_node = node _._ node; return * this;} / / --------------------------------------------------- ---------------------- // Returns a child's child node, if not, create one. xmlNode xmlNode :: get_child_node (AnsiString name) const {if (_node == NULL) return xmlNode (NULL); _di_IXMLNode node = _node-> ChildNodes-> FindNode (name); if (node == NULL) {return xmlNode (_node -> addchild (name));} else return xmlnode (node);} // ------------------------------- -------------------------------------- // Return Name is the child node of Name. XMLNode XMLnode :: Find_child_Node (Ansistring Name) const {if (_node == null) Return XMLNode (null); return XMLNode (_Node-> childnodes-> findnode (name));} // -------- -------------------------------------------------- ------------- Ansistring XMLnode :: Name () const {f (_node == null) Return Ansistring (); return_node-> nodename;} // ------- -------------------------------------------------- -------------- Ansistring XMLnode :: Value () const // gets the string value of the node. {IF (_node == null) Return Ansistring (); ANSISTRING S; try {olevariant v = _node-> text; if (v.isnull ()) s = ansistring (); else s = ansistring (v);} catch (...) {s = ansistring ();} return s;} // ------------------------------- ---------------------------------------- Ansistring XMLNode :: Child_Value (Ansistring Name) Const // Return the value of the child node. {IF (_node == null) Return Ansistring (); Ansistring S; try {olevariant v = _node-> childvalues [name]; if (v.isnull ()) s = ansistring (); else s = ansistring (v) } CatCh (...) {s = ansistring ();} return s;} // -------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------ Void XMLnode :: set_value (Ansistring Value_) const {if (_node == null) return; valid (value_); try {_node-> text = value_;} catch (...) {}} // ----------- -------------------------------------------------- ---------- Void XMLnode :: set_child_value (ANSISTRING NAME, ANSISTRING VALUE) const {i (_node == null) return; valid (value); const_di_ixmlnode node = _node-> childnodes-> findnode Name); if (node == null) {if (value.Length () == 0); else {try {_node-> addchild (name) -> text = value;} catch (...) {}} } Else {if (value.length () == 0) {try {_node-> childnodes-> remove (node);} catch (...) {}}} Else {try {_node-> childues [name] = value;} catch (...) {}}}} // ------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------ ANSISTRING XMLnode :: attribute (ANSISTRING NAME) const {ix (_node == null) Return Ansistring (); Ansistring S; try {olevariant v = _node-> attributes [name]; if (v.isnull ()) s = ansistring ); Else s = ansistring (v);} catCH (...) {s = ansistring ();} return s; } // ----------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ Void XMLnode :: set_attribute (ANSISTRING NAME, ANSISTRING VALUE) const {i (_node == null) Return; Valid ); If (value.Length () == 0) {Try {_node-> attributes [name] = null ();} catch (...) {}} else {Try {_node-> attributes [name] = Value;} catch (...) {}}} // ----------------------------------- ------------------------------------ Void XMLNode :: Find_Child_Nodes (Vector Void xmlnode :: set_attrs (const map xml version = "1.0" encoding = "utf-16"?> Void func () {MYXML XML; XML.INIT ("D: //1.xml"); XMLNode root = Xml.Root (); xmlnode node = root.get_child_node ("transcript"); node.set_attribute (" Chinese, "98"); node.set_attribute ("Mathematics", "108"); node.set_attribute ("English", "88"); node = root.get_child_node ("Weight"); Node.Set_Value 67 "); root.set_child_value (" Head "," 180 "); Xml.save ();} // ------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------- // --- -------------------------------------------------- ---------------- Premature: 1, Build Before, you must put an XMLDocument control (on the Internet page) on a form in the program, then fully compile (Project-> Options-> Linker-> Use Dynamic RTL, Project-> Options-> packages-> Build with runtime packages, two pairs of 去), remove this XMLDocument control after the compilation is complete. 2. Since the initialization of MyXML is dependent on the XML parser of the program, the initialization of the parser is completed after the program is started (after the WinMain function is started), the MyXML (ANSISTRING) constructor and myxml.init () Functions cannot be called before WinMain ().啰 These only one thing is: You can't define a global XML variable: MyXML XML ("D: //test//tt.xml); // In this case XML does not initialize correctly. / / -------------------------------------------------------------------------------------------- --------------------- // --------------------------- ------------------------------------------