tinyxml2使用

项目中遇到一个问题,C/C++需要与JAVA通信,JAVA方已经使用了XML序列化传输。本可以考虑JSON/GOOGLE PROTOCOL BUFFER的,但为了使JAVA方不做过多改动,坚持使用XML。

JAVA中可以将object与xml互转,但C/C++中没有找到类似开源项目,自己实现不太现实,只能一步一步对xml进行解析和生成了。

tinyxml2库使用非常简单,代码中引用tinyxml2.h,编译时加入tinyxml2.cpp即可;实例程序中有简单的xml解析例子。

以下对java中list/map(转xml)进行了解析:

#include <stdio.h>
#include "tinyxml2.h"
using namespace std;
using namespace tinyxml2;
        int
main ()
{
        static const char *xml =
                "<BusinessTask>"
                "<taskId>415042210020001</taskId>"
                "<controlSysId>hyb-product</controlSysId>"
                "<gwAccount>324345543543</gwAccount>"
                "<serviceKey>ba827bcfba932953</serviceKey>"
                "<type>54354675</type>"
                "<resourceNum></resourceNum>"
                "<participants>"
                "<TaskItem>"
                "<role>1</role>"
                "<participant>123124342432</participant>"
                "<startTime>0</startTime>"
                "<endTime>0</endTime>"
                "<isFinished>false</isFinished>"
                "</TaskItem>"
                "<TaskItem>"
                "<role>2</role>"
                "<participant>54675764656</participant>"
                "<startTime>0</startTime>"
                "<endTime>0</endTime>"
                "<isFinished>false</isFinished>"
                "</TaskItem>"
                "</participants>"
                "<propertyMap>"
                "<entry>"
                "<string>resourceNum</string>"
                "<string>423424424</string>"
                "</entry>"
                "<entry>"
                "<string>userId</string>"
                "<string>564657657567</string>"
                "</entry>"
                "<entry>"
                "<string>maxRetryTimes</string>"
                "<string>0</string>"
                "</entry>"
                "</propertyMap>"
                "<beginTime>1429668120</beginTime>"
                "<extParam></extParam>" "</BusinessTask>";
        /*
         * static const char* xml =
         * "<?xml version=\"1.0\"?>"
         * "<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
         * "<PLAY>"
         * "<TITLE>A Midsummer Night's Dream</TITLE>"
         * "</PLAY>";
         * */
        XMLDocument doc;
        doc.Parse (xml);
        if (doc.ErrorID () != 0)
        {
                fprintf (stderr, "parse error");
                exit (1);
        }
        XMLElement *partElement =
                doc.FirstChildElement ("BusinessTask")->
                FirstChildElement ("participants");
        XMLElement *taskitemElement = partElement->FirstChildElement ("TaskItem");
        while (taskitemElement)
        {
                int role = -1;
                XMLElement *roleElement = taskitemElement->FirstChildElement ("role");
                roleElement->QueryIntText (&role);
                fprintf (stdout, "role=%d\n", role);
                taskitemElement = taskitemElement->NextSiblingElement ();
        }
        XMLElement *propertyMapElement =
                doc.FirstChildElement ("BusinessTask")->FirstChildElement ("propertyMap");
        XMLElement *entryElement = propertyMapElement->FirstChildElement ("entry");
        while (entryElement)
        {
                XMLElement *stringElement = entryElement->FirstChildElement ();
                printf ("%s\n", stringElement->GetText ());
                stringElement = stringElement->NextSiblingElement ();
                printf ("%s\n", stringElement->GetText ());
                entryElement = entryElement->NextSiblingElement ();
        }

        return 0;
}

以下是生成XML:

#include <stdio.h>
#include <string>
#include "tinyxml2.h"
using namespace std;
using namespace tinyxml2;
int main()
{
        //xml文档
        XMLDocument *pDoc = new XMLDocument();
        if (NULL==pDoc) {
                fprintf(stderr,"pDoc is null");
                exit(1);
        }

        XMLElement* BusinessTask = pDoc->NewElement("BusinessTask");
        pDoc->LinkEndChild(BusinessTask);

        XMLElement *taskIdElement = pDoc->NewElement("taskId");
        taskIdElement->LinkEndChild(pDoc->NewText("415042210020001"));//给节点设置值
        BusinessTask->LinkEndChild(taskIdElement);

        XMLElement *controlSysIdElement = pDoc->NewElement("controlSysId");
        controlSysIdElement->LinkEndChild(pDoc->NewText("415042210020001"));//给节点设置值
        BusinessTask->LinkEndChild(controlSysIdElement);

        XMLElement *typeElement = pDoc->NewElement("type");
        typeElement->LinkEndChild(pDoc->NewText("68720525312"));//给节点设置值
        BusinessTask->LinkEndChild(typeElement);

        XMLElement *participantsElemet = pDoc->NewElement("participants");
        BusinessTask->LinkEndChild(participantsElemet);
        int i=0;
        for(i;i<3;i++)
        {
                XMLElement *TaskItemElemet = pDoc->NewElement("TaskItem");
                participantsElemet->LinkEndChild(TaskItemElemet);

                //role
                XMLElement *roleElement = pDoc->NewElement("role");
                roleElement->LinkEndChild(pDoc->NewText("1"));
                TaskItemElemet->LinkEndChild(roleElement);

                //participant
                XMLElement *participantElement = pDoc->NewElement("participant");
                participantElement->LinkEndChild(pDoc->NewText("13996130360"));
                TaskItemElemet->LinkEndChild(participantElement);
        }

        //propertyMap
        XMLElement *propertyMapElemet = pDoc->NewElement("propertyMap");
        BusinessTask->LinkEndChild(propertyMapElemet);
        for(i=0;i<3;i++)
        {
                XMLElement *entryElemet = pDoc->NewElement("entry");
                propertyMapElemet->LinkEndChild(entryElemet);

                //key
                XMLElement *keyElement = pDoc->NewElement("string");
                keyElement->LinkEndChild(pDoc->NewText("resourceNum"));
                entryElemet->LinkEndChild(keyElement);

                //value
                XMLElement *valueElement = pDoc->NewElement("string");
                valueElement->LinkEndChild(pDoc->NewText("13996130361"));
                entryElemet->LinkEndChild(valueElement);

        }

        /*
           XMLPrinter printer;
           pDoc->Accept(&printer);
           char body[8192]={0x00};
           int len = strlen(printer.CStr());
           memcpy(body,printer.CStr(),8192);
           printf("len=%d\n%s\n",len,body);*/

        pDoc->Print();//打印
        delete pDoc;
}

输出如下
415042210020001
415042210020001
68720525312

1
13996130361

1
13996130361

1
13996130361

resourceNum
13996130361

resourceNum
13996130361

resourceNum
13996130361

posted on 2017-12-21 10:43  angry-baby  阅读(994)  评论(0编辑  收藏  举报

导航