tinyxml 开源精简版本xml 解析库,相比于libxml2 更适合使用于嵌入式设备当中xml数据的解析和构造
TiXmlBase Class Reference
下载TinyXML的网址:
http://www.grinninglizard.com/tinyxml/
cp tinyxml /home/work/tinyxml
cd /home/work/tinyxml/
vim Makefile
28 DEBUG_CFLAGS := -Wall -Wno-format -g -DDEBUG -fpic -shared # 添加 -fpic -shared 生成动态库参数
29 RELEASE_CFLAGS := -Wall -Wno-unknown-pragmas -Wno-format -O3 -fpic -shared # 添加 -fpic -shared 生成动态库参数
84 OUTPUT := libtinyxml.so # 设置输出lib库
93 SRCS := tinyxml.cpp tinyxmlparser.cpp tinyxmlerror.cpp tinystr.cpp # 删掉原本编译进去的xmltest.cpp
105 ${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS} ${CXXFLAGS} # 加入${CXXFLAGS} 参数,输出最终lib库
make
22 CC := arm-linux-gcc
23 CXX := arm-linux-g++
24 LD := arm-linux-g++
25 AR := arm-linux-ar rc
test.xml 内容如下:
以上xml数据都是以
对应的解析程序如下:
#include
#include "tinyxml.h"
int test_xml(const char *xmlfile)
{
TiXmlDocument* myDocument = new TiXmlDocument();
myDocument->LoadFile(xmlfile);
TiXmlElement* rootElement = myDocument->RootElement();
if (rootElement == NULL || strcmp(rootElement->Value(), "Message"))
return -1;
printf("%s:\t %s \n", rootElement->Value(), rootElement->GetText());
TiXmlElement* element = rootElement->FirstChildElement();
if (element == NULL || strcmp(element->Value(), "MessageId"))
return -1;
printf("%s:\t %s \n", element->Value(), element->GetText());
while(1){
element = element->NextSiblingElement();
if(element == NULL){
break;
}
printf("%s:\t%s\n", element->Value(), element->GetText());
}
myDocument->Clear();
delete myDocument;
}
int main(int argc, char* argv[])
{
if(argc<2){
printf("input xml file\n");
return -1;
}
test_xml((const char *)argv[1]);
return 0;
}
xml数据格式如下:
Book store!
book1
book2
解析源码:
#include
#include
#include
#include "tinyxml.h"
using namespace std;
int loadXML(const char *xmlfile)
{
TiXmlDocument doc;
if(!doc.LoadFile(xmlfile)){
cerr << doc.ErrorDesc() << endl;
return FAILURE;
}
TiXmlElement* root = doc.FirstChildElement();
if(root == NULL)
{
cerr << "Failed to load file: No root element." << endl;
doc.Clear();
return FAILURE;
}
for(TiXmlElement* elem = root->FirstChildElement(); elem != NULL; elem = elem->NextSiblingElement())
{
string elemName = elem->Value();
const char* attr;
attr = elem->Attribute("priority");
printf("attr = %s\n",attr);
if(strcmp(attr,"1")==0)
{
TiXmlElement* e1 = elem->FirstChildElement("bold");
TiXmlNode* e2=e1->FirstChild();
cout<<"priority=1\t"<
}
else if(strcmp(attr,"2")==0)
{
TiXmlNode* e1 = elem->FirstChild();
cout<<"priority=2\t"<
}
doc.Clear();
return 0;
}
int main(int argc, char* argv[])
{
if(argc<2){
printf("input xml file\n");
return -1;
}
if(loadXML(argv[1]))
return -1;
return 0;
}
#include
#include
#include
#include
#include "tinyxml.h"
using namespace std;
int saveXML(const char *file){
TiXmlDocument doc;
TiXmlElement* root = new TiXmlElement("root");
doc.LinkEndChild(root);
TiXmlElement* element1 = new TiXmlElement("Element1");
root->LinkEndChild(element1);
element1->SetAttribute("attribute1", "some value");
TiXmlElement* element2 = new TiXmlElement("Element2"); ///元素
root->LinkEndChild(element2);
element2->SetAttribute("attribute2", "2");
element2->SetAttribute("attribute3", "3");
TiXmlElement* element3 = new TiXmlElement("Element3");
element2->LinkEndChild(element3);
element3->SetAttribute("attribute4", "4");
TiXmlText* text = new TiXmlText("Some text."); ///文本
element2->LinkEndChild(text);
bool success = doc.SaveFile((char *)file);
doc.Clear();
if(success)
return 0;
else
return -1;
}
int main(int argc, char* argv[]){
if(argc<2){
printf("input save file \n");
return -1;
}
if(saveXML((const char *)argv[1]))
return -1;
printf("save xml ok \n");
return 0;
}
生成的xml内容如下: