Tinyxml addition, deletion, and query node subnode

Source: Internet
Author: User

Header file

# Ifndef readwritexml_h
# Define readwritexml_h
# Pragma warning (Disable: 4786)
# Include "tinyxml. H"
# Include <map>
# Include <list>
Using namespace STD;

Enum errcode {
Err_paramer_null,
Err_find_null,
Err_no_son,
Err_no_root,
Err_node_exist,
Err_suecess,
};

Class creadwritexml
{
Public:
// Constructor
Creadwritexml ();

// Destructor
Virtual ~ CReadWriteXml ();

// Initialize the Function
Bool Init ();

// Read all tags of the specified type
ERRCODE ReadAllElement (const char * nType, map <string, string> & OutMap );

// Read the subnode of the specified tag
Bool ReadSubElement (const char * nType, const char * pTagetStr, const char * pSourceStr, list <string> & OutList );

// Add a node
Bool WriteElement (const char * nType, const char * pTagetStr, const char * pSourceStr );

// Add a subnode
Bool WriteSubElement (const char * nType, const char * pTagetStr, const char * pSourceStr, const char * pText );

// Delete the specified Node
Void DeleteElement (const char * nType, const char * pTagetStr, const char * pSourceStr );

// Delete a subnode
Bool CReadWriteXml: DeleteSubElement (const char * nType, const char * pTagetStr, const char * pSourceStr, const char * pText );

// Check whether the node exists in xml
TiXmlElement * FindElement (const char * nType, const char * pTagetStr, const char * pSourceStr );

// Member variable
TiXmlDocument m_ReadWriteXml;

Public:
// Store the list of ini subnodes
List <string> m_IniList;
};
# Endif

Resource file

# Include "ReadWriteXml. h"
# Include "tinyxml. h"
# Include <string>
# Include <iostream>
Using namespace std;

# Define INI_TARGETFILE "targetfile" // target file
# Define INI_SOURCEFILE "sourcefile" // compare files
# Define INIT_XMLPATH "E :\\ various learning projects \ filesearch_yly \ Procject_Test \ ProjectTest \ xml \ ini2.xml"

CReadWriteXml: CReadWriteXml (): m_ReadWriteXml ()
{

}
// Destructor
CReadWriteXml ::~ CReadWriteXml ()
{

}
// Initialize the Function
Bool CReadWriteXml: Init ()
{
M_ReadWriteXml.LoadFile (INIT_XMLPATH );
If (m_ReadWriteXml.Error ())
{
Return false;
}
Return true;
}

// Read all nodes
ERRCODE CReadWriteXml: ReadAllElement (const char * nType, map <string, string> & OutMap)
{
If (NULL = nType)
{
// Cout <"error type" <endl;
Return ERR_PARAMER_NULL;
}

TiXmlElement * RootElement = m_ReadWriteXml.RootElement ();
Const char * pTagetStr = NULL;
Const char * pSourceStr = NULL;
If (NULL = RootElement)
{
// Cout <"no elem" <endl; // for Test;
Return ERR_NO_ROOT;
}

TiXmlElement * FirstPerson = RootElement-> FirstChildElement (nType );
TiXmlElement * Element = FirstPerson;
For (; Element = Element-> NextSiblingElement (nType ))
{
// Obtain tag attributes
PTagetStr = Element-> Attribute (INI_TARGETFILE );
PSourceStr = Element-> Attribute (INI_SOURCEFILE );
// Determine whether the obtained tag is a required tag
If (! (PTagetStr & pSourceStr ))
{
Continue;
}
Else
{
// ReadSubElement (nType, Element );
OutMap. insert (make_pair (pTagetStr, pSourceStr ));
}
}
Return ERR_SUECESS;
}

Bool CReadWriteXml: ReadSubElement (const char * nType, const char * pTagetStr, const char * pSourceStr, list <string> & OutList)
{
If (NULL = nType | NULL = pTagetStr | NULL = pSourceStr) // The input parameter returned by the error code is NULL.
{
Return false;
}

TiXmlElement * pFind = FindElement (nType, pTagetStr, pSourceStr); // The Error Code indicates that the node cannot be found,
If (NULL = pFind)
{
Return false;
}
TiXmlElement * pSon = pFind-> FirstChildElement (); // The error code should be used to indicate that there are no subnodes rather than reading failed
If (NULL = pSon)
{
Return false;
}
While (pSon)
{
 
OutList. push_back (pSon-> GetText ());
PSon = pSon-> NextSiblingElement ();
}
Return true;
}

Bool CReadWriteXml: WriteElement (const char * nType, const char * pTagetStr, const char * pSourceStr)
{
If (NULL = nType | NULL = pTagetStr | NULL = pSourceStr) // The input parameter returned by the error code is NULL.
{
Return false;
}

TiXmlElement * pRootTinyXml = m_ReadWriteXml.RootElement ();
If (NULL = pRootTinyXml)
{
Return false;
}
TiXmlElement * pTmp = FindElement (nType, pTagetStr, pSourceStr); // return the error code of the node not found
If (NULL! = PTmp)
{
Return false;
}

TiXmlElement * Element = new TiXmlElement (nType );
Element-> SetAttribute (INI_TARGETFILE, pTagetStr );
Element-> SetAttribute (INI_SOURCEFILE, pSourceStr );
M_ReadWriteXml.RootElement ()-> LinkEndChild (Element );
M_ReadWriteXml.SaveFile (INIT_XMLPATH );
Return true;
}

TiXmlElement * CReadWriteXml: FindElement (const char * nType, const char * pTagetStr, const char * pSourceStr)
{
If (null = ntype | null = ptagetstr | null = psourcestr) // The input parameter returned by the error code is null.
{
Return NULL;
}

Const char * psztagetstr = NULL;
Const char * pszsourcestr = NULL;

Tixmlelement * element = m_readwritexml.rootelement ()-> firstchildelement (ntype );
For (; element = element-> nextsiblingelement (ntype ))
{
// Obtain tag attributes
Psztagetstr = element-> attribute (ini_targetfile );
Pszsourcestr = element-> attribute (ini_sourcefile );
// Determine whether the obtained tag is a required tag
// If (* pTagetStr = * pszTagetStr) & (* pSourceStr = * pszSourceStr ))
If (string (pTagetStr) = string (pszTagetStr) & string (pSourceStr) = string (pszSourceStr ))
{
Return Element;
}
Else
{
Continue;
}
}
Return NULL;
}

// Write the child node
Bool CReadWriteXml: WriteSubElement (const char * nType, const char * pTagetStr, const char * pSourceStr, const char * pText)
{
// Obtain the child node to see if the node to be added is in it
If (NULL = nType | NULL = pSourceStr | NULL = pTagetStr | NULL = pText)
{
// Cout <"error type! "<Endl;
Return false;
}
List <string> OutList;
TiXmlElement * pElement = FindElement (nType, pTagetStr, pSourceStr );
If (NULL = pElement)
{
Return false;
}
ReadSubElement (nType, pTagetStr, pSourceStr, OutList );
List <string >:: iterator it;
For (it = OutList. begin (); it! = OutList. end (); ++ it)
{
If (string (pText) = string (it-> c_str ()))
{
// Cout <"the node you want to add already exists! "<Endl;
Return false;
}
}

TiXmlElement * AddElement = new TiXmlElement ("content ");
PElement-> LinkEndChild (AddElement );
Tixmltext * xmltxt = new tixmltext (ptext );
Addelement-> linkendchild (xmltxt );

// Proot-> insertafterchild (proot-> firstchildelement (gettypename (ntype), * addelement );
M_readwritexml.savefile (init_xmlpath );
Return true;
}

Void creadwritexml: deleteelement (const char * ntype, const char * ptagetstr, const char * psourcestr)
{
Tixmlelement * proot = m_readwritexml.rootelement ();
Tixmlnode * prootnode = proot-> toelement ();
Tixmlelement * pdelete = findelement (ntype, ptagetstr, psourcestr );
If (null = pdelete)
{
Return;
}
TiXmlNode * pNodeDelete = pDelete-> ToElement ();
PRoot-> RemoveChild (pNodeDelete );
M_ReadWriteXml.SaveFile (INIT_XMLPATH );
}

Bool CReadWriteXml: DeleteSubElement (const char * nType, const char * pTagetStr, const char * pSourceStr, const char * pText)
{
TiXmlElement * pDelete = FindElement (nType, pTagetStr, pSourceStr );
If (NULL = pDelete)
{
// Cout <"the node you want to delete does not exist" <endl;
Return false;
}
TiXmlElement * pRoot = m_ReadWriteXml.RootElement ();
TiXmlElement * pTmp = pDelete-> FirstChildElement ();
While (pTmp)
{
If (string (pTmp-> GetText () = string (pText ))
{
Break;
}
PTmp = pTmp-> NextSiblingElement ();
}
If (NULL = pTmp)
{
// Cout <"z cannot find the node you want to delete" <endl;
Return false;
}
// While
TiXmlNode * pDelNode = pTmp-> ToElement ();
If (NULL = pDelNode)
{
Cout <"the node you want to delete is not found" <endl;
Return false;
}
PDelete-> RemoveChild (pDelNode );
M_ReadWriteXml.SaveFile (INIT_XMLPATH );
Return true;
}

 

 

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.