tinyxml的使用以及示例

本文介绍了一个使用TinyXML库解析XML字符串的例子。通过具体代码演示了如何解析不同类型的XML数据,并展示了如何利用TinyXML进行属性查询及节点遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.下载xml源代码:github上面 clone

地址:https://github.com/aughey/tinyxml/

2.下来以后自己make一下主要的就几个头文件和源文件

3.测试案例

4.编译成静态库: ar rv libxml.a  *.o

5. 测试案例运行: g++ -o test test.o -L./ -lxml

6.问题:对于main程序,先编译成目标文件,最后链接成库,不能一步做完

主要过程


#include <iostream>
#include <string>
#include "tinyxml.h"
#include "myxml.h"
#include <assert.h>
using namespace std;

int main(int argc, const char *argv[])
{
    string xml_str = "<Class name=\"计算机软件班\">  \
                      <Students>           \
                      <student name=\"张三\" studentNo=\"13031001\" sex=\"男\" age=\"22\">  \
                      <phone>88208888</phone>     \
                      <address>西安市太白南路二号</address>  \
                      </student>    \
                      <student name=\"李四\" studentNo=\"13031002\" sex=\"男\" age=\"20\">  \
                      <phone>88206666</phone>   \
                      <address>广东省深圳市</address>  \
                      </student>   \
                      </Students>  \
                      </Class>";

    string msg_xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>       \
                      <returnsms>   \
                      <returnstatus>Success</returnstatus>      \
                      <message>操作成功</message>     \
                      <remainpoint>3</remainpoint>            \
                      <taskID>1412291000299729</taskID>       \
                      <successCounts>1</successCounts>       \
                      <returnstatus>Success</returnstatus>      \
                      <message>操作成功</message>     \
                      <remainpoint>1</remainpoint>            \
                      <taskID>111191000299729</taskID>       \
                      <successCounts>1</successCounts>       \
                      </returnsms>";

    string rsp_msg = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>   \
                      <returnsms>   \
                      <statusbox>     \
                      <mobile>13045877691</mobile>    \
                      <taskid>1412261437053252</taskid>   \
                      <status>10</status>     \
                      <receivetime>2014-12-26 14:37:08</receivetime>      \
                      <errorcode>DELIVRD</errorcode>      \
                      <extno></extno>     \
                      </statusbox>      \
                      <statusbox>     \
                      <mobile>13045877691</mobile>      \
                      <taskid>1412291000299729</taskid>   \
                      <status>10</status>     \
                      <receivetime>2014-12-29 10:00:32</receivetime>        \
                      <errorcode>DELIVRD</errorcode>        \
                      <extno></extno>       \
                      </statusbox>        \
                      </returnsms>";

        mylib::MyXml my_xml;
        mylib::MyXml::MapMap map_value;
   /*     my_xml.MultiParse(rsp_msg, map_value);
        cout << map_value[0]["mobile"] << endl;
        cout << map_value[1]["mobile"] << endl;
        cout << map_value[0]["taskid"] << " " << map_value[1]["taskid"] << endl;
*/
        my_xml.SignalParse(msg_xml, map_value);
        cout << "taskid:" << map_value[0]["taskID"] << endl;
/*
    TiXmlDocument* myDocument = new TiXmlDocument();
    //myDocument->LoadFile("Students.xml");

//解析多个

    myDocument->Parse(rsp_msg.c_str());
    TiXmlNode* pXmlFirst = myDocument->FirstChild();
    TiXmlDeclaration* pXmlDec = pXmlFirst->ToDeclaration();//获取头部声明
    cout << "szVer:" << pXmlDec->Version()
        << " is absolute:" << pXmlDec->Standalone()
        << " code type:" << pXmlDec->Encoding()
        << endl;

    myDocument->Print();
    TiXmlElement* rootElement = myDocument->RootElement();//returnsms
   // TiXmlElement* everyEle = rootElement->FirstChildElement();
    TiXmlElement* pEle = NULL;
    for(pEle = rootElement->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
    {
     //   cout << "1" << endl;

       // TiXmlElement* pEle_child = pEle->FirstChildElement();
       // cout << "1:" << pEle_child->GetText() << endl;

      //   pEle_child = pEle_child->NextSiblingElement();
      //  cout << "2:" << pEle_child->GetText() << endl;

    TiXmlElement* pEle_child;
    //cout << pEle->FirstAttribute()->Name() << ":";
    string rsp;
    pEle->QueryValueAttribute("mobile", &rsp);
    cout << "QueryValueAttribute:" <<  rsp << endl;

   for(pEle_child = pEle->FirstChildElement(); pEle_child; pEle_child = pEle_child->NextSiblingElement())
    {
        cout << pEle_child->Value() << ":"<< endl;

          string value;
          if(pEle_child->GetText())
          {
              sp = pEle_child->GetText();
          }
          cout << value << endl;
    }

      // cout << pEle->GetText() << endl;
    }

    //ReadXmlText
    //解析单个msg_xml

    myDocument->Parse(msg_xml.c_str());
    TiXmlNode* pXmlFirst = myDocument->FirstChild();
    TiXmlDeclaration* pXmlDec = pXmlFirst->ToDeclaration();//获取头部声明
    cout << "szVer:" << pXmlDec->Version()
        << " is absolute:" << pXmlDec->Standalone()
        << " code type:" << pXmlDec->Encoding()
        << endl;
    TiXmlElement* rootElement = myDocument->RootElement();

  //  TiXmlNode* pParent = rootElement->Parent();
  //  TiXmlNode* pChild = rootElement->FirstChild();

    TiXmlElement* pEle = NULL;
    for(pEle = rootElement->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
    {
        cout << "\t";
        cout << pEle->GetText() << endl;

    }
*/

    //解析xml_str
    /* myDocument->Parse(xml_str.c_str());
       TiXmlElement* rootElement = myDocument->RootElement();  //Class
       TiXmlElement* studentsElement = rootElement->FirstChildElement();  //Students
       TiXmlElement* studentElement = studentsElement->FirstChildElement();  //Students
       while ( studentElement) {
       TiXmlAttribute* attributeOfStudent = studentElement->FirstAttribute();  //获得student的name属性
       while ( attributeOfStudent ) {
       std::cout << attributeOfStudent->Name() << " : " << attributeOfStudent->Value() << std::endl;
       attributeOfStudent = attributeOfStudent->Next();
       }

       TiXmlElement* phoneElement = studentElement->FirstChildElement();//获得student的phone元素
       std::cout << "phone" << " : " << phoneElement->GetText() << std::endl;

       TiXmlElement* addressElement = phoneElement->NextSiblingElement();
       std::cout << "address" << " : " << addressElement->GetText() << std::endl;

       studentElement = studentElement->NextSiblingElement();
       }
       */

  //     delete myDocument;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值