CMarkup 入门(增删改查)

CMarkup是一个比较好的C++ xml解析库。
官网:http://www.firstobject.com/dn_markup.htm
主要操作代码:

// xml_CMarkup.cpp : 定义控制台应用程序的入口点。
//
//CRUD 增加(Create)、读取(Read)、更新(Update)和删除(Delete)
#include "stdafx.h"
#include "Markup.h"
#include <iostream>
#include <string>
using namespace std;
#define _CREATE
//#define _ADD
//#define _DELETE
//#define _UPDATE
//#define _READ
int _tmain(int argc, _TCHAR* argv[])
{
#ifdef _CREATE
    //创建一个 XML Document
    CMarkup xml;
    xml.AddElem("ORDER");//unicode 需要加: _T()或_TEXT
    xml.IntoElem();
    xml.AddElem("ITEM");
    xml.IntoElem();
    xml.AddElem("SN", "132487A-J");
    xml.AddElem("NAME", "crank casing");
    xml.AddElem("QTY", "1");
    xml.Save("d:\\UserInfo.xml");//保存到文件
    //-------------------------
    string strXML = xml.GetDoc();//读文件内容到字符串 (MCD_STR or std::string or CString)
    cout << strXML << endl;
#endif
#ifdef _ADD
    //增加元素和属性
    //添加在最后面, 使用的是AddElem; 添加在最前面, 使用InsertElem。
    CMarkup xml;
    if (!xml.Load("d:\\UserInfo.xml"));
    //  return 1;
    xml.AddElem("ORDER");
    xml.IntoElem(); // 进入 ORDER

    xml.AddElem("ITEM");
    xml.IntoElem(); // 进入 ITEM
    xml.AddElem("SN", "4238764-A"); //添加元素
    xml.AddElem("NAME", "bearing");//添加元素
    xml.AddElem("QTY", "15");//添加元素
    xml.OutOfElem(); // 退出 ITEM 
    xml.AddElem("SHIPMENT");
    xml.IntoElem(); // 进入 SHIPMENT
    xml.AddElem("POC");//添加元素
    xml.SetAttrib("type", "non-emergency");//添加属性
    xml.IntoElem(); // 进入 POC
    xml.AddElem("NAME", "John Smith");//添加元素
    xml.AddElem("TEL", "555-1234");//添加元素
    xml.Save("d:\\UserInfo.xml");
#endif
#ifdef _DELETE
    //删除元素: 删除SN = 132487A - J的项目。
    CMarkup xml;
    xml.Load("d:\\UserInfo.xml");
    string strUserID = _T("");
    xml.ResetMainPos();
    if (xml.FindChildElem("ITEM"))
    {
        xml.IntoElem();
        string str_sn;
        xml.FindChildElem("SN");
        str_sn = xml.GetChildData();
        if (str_sn == "132487A-J")
        {
            xml.RemoveElem();
            xml.Save("d:\\UserInfo.xml");
        }
    }
#endif
#ifdef _UPDATE
    //修改元素和属性---------
    CMarkup xml;
    if (xml.Load("d:\\UserInfo.xml"))//格式不正确可能导致load失败
    {
        string strUserID = _T("");
        xml.ResetMainPos();
        if (xml.FindChildElem("SHIPMENT"))
        {
            xml.IntoElem();
            if (xml.FindChildElem("POC"))
            {
                xml.IntoElem();
                string str_type = xml.GetAttrib("type");

                xml.SetAttrib("type", "change");
                strUserID = xml.GetData();

                if (xml.FindChildElem("TEL"))
                {
                    xml.IntoElem();
                    xml.SetData("99999999");
                    xml.Save("d:\\UserInfo.xml");
                    return 0;
                }
            }
        }
    }
#endif
#ifdef _READ
    //查找元素
    CMarkup xml;
    xml.Load("d:\\UserInfo.xml");//From a file with Load 
    //xml.SetDoc(strXML);//Or from an XML string with SetDoc
    xml.FindElem(); // root ORDER element
    xml.IntoElem(); // inside ORDER
    while (xml.FindElem("ITEM"))
    {
        xml.IntoElem();
        xml.FindElem("SN");
        MCD_STR strSN = xml.GetData();
        cout << strSN << endl;
        xml.FindElem("QTY");
        int nQty = atoi(MCD_2PCSZ(xml.GetData()));//MCD_2PCSZ is defined in Markup.h to return the string's const pointer.
        cout << nQty << endl;
        xml.OutOfElem();
    }
    //查找元素1
    xml.IntoElem();
    xml.FindElem("SN");
    MCD_STR strSN = xml.GetData();
    xml.ResetMainPos();
    xml.FindElem("QTY");
    int nQty = atoi(MCD_2PCSZ(xml.GetData()));
    cout << nQty << endl;
    xml.OutOfElem();
    //查找元素2
    xml.ResetPos(); // top of document
    xml.FindElem(); // ORDER element is root
    xml.IntoElem(); // inside ORDER
    while (xml.FindElem("ITEM"))
    {
        xml.FindChildElem("SN");
        if (xml.GetChildData() == "4238764-A"){
            cout << "Found!" << endl;
            break; // found
        }
        else{
            cout << "Not Found!" << endl;
        }
    }
#endif  
    return 0;
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值