I wrote an XML file class that encapsulates IXMLDocument (C ++ Builder).

zhaozj2021-02-16  82

/ * 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 #include // -------------------------------- ---------------------------------------- Class XMLNode; // Character XML node Class, // Because there is cross-reference with MyXML, it is predefined here. / / -------------------------------------------------------------------------------------------- --------------------------- Class myXML // XML file class. {private: int savepause; // Used to control frequent storage ,Improve efficiency. Private: ansistring _fpath; // XML file name, containing paths. _Di_ixmldocument XML document class in XMLDoc; // CB. _Di_ixmlnode root; // root node, only one. Public: myXML (); // Constructor MYXML (ANSISUSTRING FPATH); // Constructor Void Init (ANSISTRING FPATH); / / Initializing BOOL ISNULL () const; // Is a valid void save () const; // store XMLNode Root () const; // Get root node void disable_save (); // This function is invalid. Void enable_save (); // Execute Save () operation. // These two functions are always pairs, which are used to make a large amount of // fragmented in the two functions (if any) is skipped to improve system efficiency. // These two functions are me later, there is no relationship with the XML document, // If you don't feel there is not necessary, you can go.

Void save_to (ANSISTRING FPATH_) const; // Save to another file}; // ------------------------------- ------------------------------------------ TypedEf Map AttrMap; // Used to indicate a set of properties of nodes // definition This can be a lot of words. / / -------------------------------------------------------------------------------------------- --------------------------- Class XMLnode // Character XML node class, {private: _di_ixmlnode _node; public: xmlnode (); xmlnode (_di_ixmlnode node); void init (_di_ixmlnode node); BOOL ISNULL () const; xmlnode & operator = (const Xmlnode & node_); public: Ansistring name () const; // returns the name of the node. Ansistring Attribute (ANSISUSTRING NAME) const; // Returns an attribute value. Name represents the attribute name. Ansistring value () const; // Gets the string value of the node.

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: value

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 & Attrs) const; // Get all attributes of this node once.

Void set_attrs (const map

Void find_child_nodes (Vector & nodes) const; // Get all subpatals. Void del_child_nodes () const; // Deletes all child nodes. Void add_child_node (ANSISSTRING Child_name) const; // Add a child node void del_child_node (ANSISTRING Child_Name) const; // deletes a child node. }; // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------- # ENDIF / / below is "my_xml.cpp" content: // ----- -------------------------------------------------- ------------------ // 2003-1-26 # pragma hdrstop

#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 & nodes) Const {IF (_Node == null) return; _node-> childnodes; for (int i = 0; i count; i ) {if (list-> nodes [i] -> nodetype == nthlement ) {Nodes.push_back (list-> nodes [i]);}}} // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------- // Get all attributes all in this node . Void xmlnode :: get_attrs (map attributenodes; for (int i = 0; i count; i ) {ANSISTRING ATTR_NAME = list-> nodes [i] -> nodename; ANSISUSTRING Attr_Value; try {attr_value = list-> nodes [i] -> nodeValue;} catch (...) {attr_value = Ansistring ();} attrs. INSERT (Make_Pair (attr_name, attr_value);}} // -------------------------------------------------------------------------------------------------- --------------------------------- // First remove the old properties all, the new attribute is a whole increase.

Void xmlnode :: set_attrs (const map attributenodes-> clear (); map :: const_iterator p; for ( P = attrs.begin (); p! = attrs.end (); p) {set_attribute (p-> first, p-> second);}} // ----------- -------------------------------------------------- ---------- Void XMLnode :: add_child_node (ANSISTRING Child_Name) const {if (_node == null) return; _node-> addchild (child_name);} // --------- -------------------------------------------------- ------------ Void XMLNode :: del_child_nodes () const {i (_node == null) {_node-> HaschildNodes) {_node-> childnodes-> clear ();}} / / -------------------------------------------------------------------------------------------- ----------------------- Void XMLnode :: del_child_node (Ansistring Child_name) const {if (_node == null) return; if (_node-> HaschildNodes) {_Node-> CHildNodes-> delete (child_name);}} // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------- // Since the code is not written once, it is a bit chaotic. / / -------------------------------------------------------------------------------------------- --------------------- / / =========================== End ====================================================================================================================================== Generate a such XML file.

67 180 // -------------------------------------- ----

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.

/ / -------------------------------------------------------------------------------------------- --------------------- // --------------------------- ------------------------------------------

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

New Post(0)