tinyxml解析xml文件

前端之家收集整理的这篇文章主要介绍了tinyxml解析xml文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、tinyxml 库

tinyxml 开源精简版本xml 解析库,相比于libxml2 更适合使用于嵌入式设备当中xml数据的解析和构造


TiXmlBase Class Reference




下载TinyXML的网址:

http://www.grinninglizard.com/tinyxml/


二、tinyxml 编译成.so

1、将tinyxml 拷贝到/home/work/目录下

cptinyxml /home/work/tinyxml

cd/home/work/tinyxml/


2、修改Makefile

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库


3、编译生成libtinyxml.so

make


4、交叉编译移植tinyxml 到开发板,需要修改

22 CC := arm-linux-gcc
23 CXX := arm-linux-g++
24 LD := arm-linux-g++
25 AR := arm-linux-ar rc


三、编写测试例子

1、解析xml 数据<xxxxx>data</xxxxxx> 形式存放的xml数据

test.xml 内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<Message xmlns="http://mns.aliyuncs.com/doc/v1">
<MessageId>6D438C3EE5E6E031-1-1579E5A156F-20000000E</MessageId>
<MessageBodyMD5>009F8E6EDCA8CA12AB5F82F5EBE514F7</MessageBodyMD5>
<MessageBody>{"title":"text","content":"测试消息"}</MessageBody>
<ReceiptHandle>1-ODU4OTkzNDYwNi0xNDc1ODM0MjI3LTEtOA==</ReceiptHandle>
<EnqueueTime>1475830486383</EnqueueTime>
<FirstDequeueTime>1475831421138</FirstDequeueTime>
<NextVisibleTime>1475834227000</NextVisibleTime>
<DequeueCount>2</DequeueCount>
<Priority>8</Priority>
</Message>


以上xml数据都是以 <xxxxx>data</xxxxxx> 形式存放的xml数据

对应的解析程序如下:


#include <stdio.h>
#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->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;
}


2、解析item 类型的xml数据

xml数据格式如下:

<ToDo>
<Item priority="1">
<bold>
Book store!
</bold>
</Item>
<Item priority="2">
book1
</Item>
<Item priority="2">
book2
</Item>
</ToDo>


解析源码:

#include <stdio.h>
#include <iostream>
#include <string>


#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"<<e2->ToText()->Value()<<endl;


}
else if(strcmp(attr,"2")==0)
{
TiXmlNode* e1 = elem->FirstChild();
cout<<"priority=2\t"<<e1->ToText()->Value()<<endl;
}

}
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;
}


3、生成xml数据

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#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内容如下:

<root> <Element1 attribute1="some value" /> <Element2 attribute2="2" attribute3="3"> <Element3 attribute4="4" />Some text. </Element2> </root>

猜你在找的XML相关文章