Most recently, xml is used. However, there is very little information on the Internet. I made it myself .. The tinyxml class is used. For details about how to use it, see another blog.
C ++ reads and writes Xml using TinyXml
Here, there are simple examples including compilation. The following program is just a simple query process. The specific process also needs to be completed when you are using it. I also published it after successful attempts. It may not meet your needs. If you use it, you may need to change it ....
The xml I read is
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<List>
<Dict id = "1" key = "english">
<Key> english </key>
<Acceptation> English; English language; English (UK); pure English;
</Acceptation>
<Pos> adj. </pos>
<Acceptation> English; written in English;
</Acceptation>
</Dict>
<Dict id = "2" key = "one">
<Key> one </key>
<Acceptation> (number) 1; 1; (base) 1; 1;
</Acceptation>
<Pos> n. </pos>
<Acceptation> one person; one o'clock; one person;
</Acceptation>
</Dict>
<Dict id = "3" key = "key">
<Key> key </key>
<Pos> vt. </pos>
<Acceptation> type, lock, and adjust... Provide clues. </acceptation>
</Dict>
</List>
Call entry:
[Html]
Char * file = "egxml. xml ";
TiXmlDocument xmlDoc (file );
XmlDoc. LoadFile ();
TiXmlElement * pRootElement = xmlDoc. RootElement (); // get the following Node
TiXmlElement * pElesele = selectXml (pRootElement, "2", "id"); // search nodes
String msg = "";
Msg + = dumpNode (pElesele, 0); // read all data of the node
Search for node functions
[Cpp] www.2cto.com
TiXmlElement * CegDlg: selectXml (TiXmlElement * pRootElement, string strVal, string strName) // with directory node, query attribute value, query attribute
{
TiXmlElement * pEle = NULL;
String msg = "";
TiXmlElement * reEle = NULL; // used to return
For (pEle = pRootElement-> FirstChildElement (); pEle = pEle-> NextSiblingElement () // loop all nodes below
{
// Recursive find sub node return node pointer
Int t = pEle-> Type ();
TiXmlElement * pElement = pEle-> ToElement (); // convert to a node
TiXmlAttribute * pAttr = pElement-> FirstAttribute (); // obtain the first attribute
TiXmlAttribute * pNextAttr = NULL;
If (pAttr! = NULL)
{
String tmpAttrVal = "";
String tmpAttrName = "";
Do
{
TmpAttrVal = pAttr-> Value (); // Attribute Value
TmpAttrName = pAttr-> Name (); // attribute name
If (tmpAttrVal = strVal & tmpAttrName = strName)
{
Return reEle = pElement; // if it is found, the current node is returned.
Break;
}
} While (pAttr = pAttr-> Next (); // transfer to the Next attribute
}
}
Return reEle;
}
Read all data
[Html]
String CegDlg: dumpNode (TiXmlNode * pNode, int flag)
{
String msg = "";
If (pNode = NULL)
{
Return "";
}
TiXmlText * pText = NULL;
TiXmlNode * pChildNode = NULL;
Int t = pNode-> Type ();
If (t = TiXmlText: TINYXML_TEXT) // The node type is text node.
{
PText = pNode-> ToText ();
String text = pText-> Value ();
If (! Text. empty ())
{
Msg = msg + "=" + text;
}
}
Else if (t = TiXmlText: TINYXML_ELEMENT) // The node type is Element.
{
Msg = msg + "rn ";
Int num = flag;
While (num> = 1)
{
Msg = msg + "t ";
Num --;
}
Msg = msg + pNode-> Value ();
// Output attributes
TiXmlElement * pElement = pNode-> ToElement ();
TiXmlAttribute * pAttr = pElement-> FirstAttribute ();
TiXmlAttribute * pNextAttr = NULL;
String tmpAttrMsg = "[";
If (pAttr! = NULL)
{
String tmpAttrVal = "";
String tmpAttrName = "";
Do
{
TmpAttrVal = pAttr-> Value ();
TmpAttrName = pAttr-> Name ();
TmpAttrMsg + = tmpAttrName + "=" + tmpAttrVal + ","; // each attribute is separated by a comma.
} While (pAttr = pAttr-> Next ());
TmpAttrMsg = tmpAttrMsg. erase (tmpAttrMsg. find_last_of (","));
}
TmpAttrMsg + = "]";
Msg + = tmpAttrMsg;
}
// Cyclically access each of its elements
For (pChildNode = pNode-> FirstChild (); pChildNode! = 0; pChildNode = pChildNode-> NextSibling ())
{
Msg = msg + dumpNode (pChildNode, flag + 1 );
}
Return msg;
}