字符串
代码语言:javascript复制***POP3
**LHTOOL
[工具(&LHTOOL)]
[转换]^C^CLHTC
[连接]^C^CLHBC
[--]
[->全矩形]
[矩形暗柱]^C^CLHWA
[C暗柱]^C^CLHWC
[柱]^C^CLHDZ
[L暗柱]^C^CLHWL
[T暗柱]^C^CLHWT
[Z暗柱]^C^CLHWZ
[TL暗柱]^C^CLHWJ
[F暗柱]^C^CLHWF
[暗柱]^C^CLHWS
[<-计算]^C^CLHWE
XML
代码语言:javascript复制<?xml version="1.0" ?>
<MyApp>
<!-- Settings for MyApp -->
<Messages name="工具(&LHTOOL)">
<Welcome name="转换">LHTC</Welcome>
<Farewell name="连接">CLHBC</Farewell>
<Farewell>--</Farewell>
<Farewell flag="chlid" name="->全矩形">
<Farewell name="矩形暗柱">LHWA</Farewell>
<Farewell name="C暗柱">LHWC</Farewell>
<Farewell name="柱">LHDZ</Farewell>
<Farewell name="L暗柱">LHWL</Farewell>
<Farewell name="T暗柱">LHWT</Farewell>
<Farewell name="Z暗柱">LHWZ</Farewell>
<Farewell name="TL暗柱">LHWJ</Farewell>
<Farewell name="F暗柱">LHWF</Farewell>
<Farewell name="暗柱">LHWS</Farewell>
<Farewell name="<-计算">LHWE</Farewell>
</Farewell>
</Messages>
</MyApp>
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8"?>
<list>
<emp id="2575">
<name>黄海锋</name>
<age>20</age>
<gender>man</gender>
<salary>12000</salary>
</emp>
<emp id="2576">
<name>黄海川</name>
<age>20</age>
<gender>man</gender>
<salary>12000</salary>
</emp>
<emp id="2577">
<name>黄海兰</name>
<age>20</age>
<gender>man</gender>
<salary>12000</salary>
</emp>
<emp id="2578">
<name>黄海基</name>
<age>20</age>
<gender>man</gender>
<salary>12000</salary>
</emp>
</list>
封装类
代码语言:javascript复制// tutorial demo program
#include "stdafx.h"
#include "tinyxml.h"
// ----------------------------------------------------------------------
// STDOUT dump and indenting utility functions
// ----------------------------------------------------------------------
const unsigned int NUM_INDENTS_PER_SPACE=2;
const char * getIndent( unsigned int numIndents )
{
static const char * pINDENT=" ";
static const unsigned int LENGTH=strlen( pINDENT );
unsigned int n=numIndents*NUM_INDENTS_PER_SPACE;
if ( n > LENGTH ) n = LENGTH;
return &pINDENT[ LENGTH-n ];
}
// same as getIndent but no " " at the end
const char * getIndentAlt( unsigned int numIndents )
{
static const char * pINDENT=" ";
static const unsigned int LENGTH=strlen( pINDENT );
unsigned int n=numIndents*NUM_INDENTS_PER_SPACE;
if ( n > LENGTH ) n = LENGTH;
return &pINDENT[ LENGTH-n ];
}
int dump_attribs_to_stdout(TiXmlElement* pElement, unsigned int indent)
{
if ( !pElement ) return 0;
TiXmlAttribute* pAttrib=pElement->FirstAttribute();
int i=0;
int ival;
double dval;
const char* pIndent=getIndent(indent);
printf("n");
while (pAttrib)
{
printf( "%s%s: value=[%s]", pIndent, pAttrib->Name(), pAttrib->Value());
if (pAttrib->QueryIntValue(&ival)==TIXML_SUCCESS) printf( " int=%d", ival);
if (pAttrib->QueryDoubleValue(&dval)==TIXML_SUCCESS) printf( " d=%1.1f", dval);
printf( "n" );
i ;
pAttrib=pAttrib->Next();
}
return i;
}
void dump_to_stdout( TiXmlNode* pParent, unsigned int indent = 0 )
{
if ( !pParent ) return;
TiXmlNode* pChild;
TiXmlText* pText;
int t = pParent->Type();
printf( "%s", getIndent(indent));
int num;
switch ( t )
{
case TiXmlNode::TINYXML_DOCUMENT:
printf( "Document" );
break;
case TiXmlNode::TINYXML_ELEMENT:
printf( "Element [%s]", pParent->Value() );
num=dump_attribs_to_stdout(pParent->ToElement(), indent 1);
switch(num)
{
case 0: printf( " (No attributes)"); break;
case 1: printf( "%s1 attribute", getIndentAlt(indent)); break;
default: printf( "%s%d attributes", getIndentAlt(indent), num); break;
}
break;
case TiXmlNode::TINYXML_COMMENT:
printf( "Comment: [%s]", pParent->Value());
break;
case TiXmlNode::TINYXML_UNKNOWN:
printf( "Unknown" );
break;
case TiXmlNode::TINYXML_TEXT:
pText = pParent->ToText();
printf( "Text: [%s]", pText->Value() );
break;
case TiXmlNode::TINYXML_DECLARATION:
printf( "Declaration" );
break;
default:
break;
}
printf( "n" );
for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
{
dump_to_stdout( pChild, indent 1 );
}
}
// load the named file and dump its structure to STDOUT
void dump_to_stdout(const char* pFilename)
{
TiXmlDocument doc(pFilename);
bool loadOkay = doc.LoadFile();
if (loadOkay)
{
printf("n%s:n", pFilename);
dump_to_stdout( &doc ); // defined later in the tutorial
}
else
{
printf("Failed to load file "%s"n", pFilename);
}
}
// ----------------------------------------------------------------------
// main() for printing files named on the command line
// ----------------------------------------------------------------------
int main(int argc, char* argv[])
{
for (int i=1; i<argc; i )
{
dump_to_stdout(argv[i]);
}
return 0;
}
在命令行或者DOS窗口中运行,如下:
C:devtinyxml> Debugtinyxml_1.exe example1.xml
example1.xml:
Document
Declaration
Element [Hello]
(No attributes)
Text: [World]
main
代码语言:javascript复制#include <iostream>
#include"tinyxml2.h"
#include <map>
using namespace std;
using namespace tinyxml2;
//核心代码,递归实现读取xml的节点
void GetElementValue(XMLElement *element , std::map<std::string, std::string> &itemID_value_map)
{
for (XMLElement* currenteleElement = element->FirstChildElement(); currenteleElement; currenteleElement = currenteleElement->NextSiblingElement())
{
XMLElement* tmpele = currenteleElement;
if (currenteleElement->GetText() != NULL)
{
cout << "ItemID = " << currenteleElement->Attribute("ItemID") << "---Value = " << currenteleElement->GetText() << endl;
itemID_value_map[currenteleElement->Attribute("ItemID")] = currenteleElement->GetText();
}
//判断该节点是否为空,如果不为空则继续递归
if (!tmpele->NoChildren())
GetElementValue(tmpele,itemID_value_map);
}
}
//递归读取xml文件中的节点,并且保存到map中
std::map<std::string,std::string> CycleReadAllNode(std::string xmlPath)
{
static std::map<std::string, std::string> itemID_value_map;
itemID_value_map.clear();
tinyxml2::XMLDocument xml;
if (xml.LoadFile(xmlPath.c_str()) != XML_SUCCESS)
{
return itemID_value_map; //return empty map
}
tinyxml2::XMLElement* rootNode = xml.RootElement();
if (rootNode == NULL) {
return itemID_value_map; //return empty map
}
GetElementValue(rootNode, itemID_value_map);
return itemID_value_map;
}
int main()
{
std::map<std::string, std::string> maps = CycleReadAllNode("ElectronicProduct.xml");
//遍历map
cout << "-----------------------begin 循环遍历节点------------------------" << endl;
std::map<std::string, std::string>::iterator iter;
for (iter = maps.begin(); iter != maps.end(); iter )
{
std::cout << "itemId = " << (iter->first).c_str() << "-------value = " << (iter->second).c_str() << std::endl;
}
cout << "-----------------------end 循环遍历节点------------------------" << endl;
return 0;
}
ElectronicProduct.xml
代码语言:javascript复制自己需要写的ElectronicProduct.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<ElectronicProduct>
<computer ItemID = "DELL">8000yuan</computer>
<computer ItemID = "MAC PRO">16000yuan</computer>
<pad ItemID = "ipadmin3">1600yuan</pad>
</ElectronicProduct>
代码语言:javascript复制TiXmlDocument* myDocument ;
myDocument.Parse(xmlStr));
TiXmlElement* rootElement = myDocument.RootElement();
if (rootElement)
{
TiXmlElement* element = rootElement->FirstChildElement();
if (element)
{
for (TiXmlNode* SubItem = rootElement->FirstChild();SubItem!=NULL;)
{
TiXmlElement* AppNameElement = SubItem->ToElement()->FirstChildElement();
TiXmlElement* nameElement = AppNameElement->NextSiblingElement();
TiXmlElement* ageElement = AppPermit_typeElement->NextSiblingElement();
TiXmlElement* heightElement = AppPermit_typeElement->NextSiblingElement();
TiXmlElement* weightElement = AppPermit_typeElement->NextSiblingElement();
printf("%s:t%sn", nameElement ->Value(), nameElement ->GetText());
printf("%s:t%sn", ageElement ->Value(), ageElement ->GetText());
printf("%s:t%sn", heightElement ->Value(), heightElement ->GetText());
printf("%s:t%sn", weightElement ->Value(), weightElement ->GetText());
SubItem=SubItem->NextSibling();
}
}
}
代码语言:javascript复制TiXmlDocument myDocument;
myDocument.Parse(xmlStr);
TiXmlElement* rootElement = myDocument.RootElement();
if (rootElement)
{
TiXmlElement* element = rootElement->FirstChildElement();
if (element)
{
for (TiXmlNode* SubItem = rootElement->FirstChild();SubItem!=NULL;)
{
TiXmlElement* ExtNameElement = SubItem->ToElement();
printf("%s:t%sn", NameElement->Value(), NameElement->GetText());
SubItem=SubItem->NextSibling();
}
}
}
}