TinyXml库的基本操作

#ifndef _PARSE_XML_H_
#define _PARSE_XML_H_

#include "tinyxml.h"
#include "tinystr.h"
#include <iostream>

void CreateNode(TiXmlNode *ParentNode, const char *NodeName, const char *NodeText,
                const char *AttributeName, const char *AttributeValue);
void CreateXmlFile();
TiXmlNode * selectChildNode(TiXmlNode * pNode, std::string nodeName, 
std::string nodeAttrName, std::string nodeAttrValue);
TiXmlNode * SelectSingleNodeByRootEle(TiXmlElement* RootElement,const char *nodeName,
const char *nodeAttrName,const char *nodeAttrValue);
void ModifyXmlFile(TiXmlElement* element, const char *NewText, const char *NewAttributeName,
                   const char *NewAttributeValue);
TiXmlNode * AddElement(TiXmlElement *position, const char *ElementName, const char *ElementText,
                const char *AttributeName, const char *AttributeValue,
                const char *SecondAttributeName, const char *SecondAttributeValue);
void DeleteElement(TiXmlNode *pNode);
void ReadXmlElement(TiXmlElement *pCurrentElement);
const char *GetAttributeValue(TiXmlNode *pNode, const char *name);

#endif

#include "parse_xml.h"
/**
* @breif: 插入节点
* @param pPRE_Node: 父节点
* @param NodeName: 节点名字
* @param NodeText: 节点内容
* @param AttributeName: 属性名字
* @param AttributeValue: 属性值
* @return value: NO
*/
void CreateNode(TiXmlNode *ParentNode, const char *NodeName, const char *NodeText,
                const char *AttributeName, const char *AttributeValue)
{
    TiXmlElement* pElement = new TiXmlElement(NodeName);
    TiXmlText* pText = new TiXmlText(NodeText);
    pElement->LinkEndChild(pText);
    if (AttributeName != NULL && AttributeValue != NULL)
    {
        pElement->SetAttribute(AttributeName, AttributeValue);
    }       
    ParentNode->ToElement()->LinkEndChild(pElement);

    return ;
}

void CreateXmlFile()
{
    TiXmlDocument *pdoc = new TiXmlDocument("test.xml");
    if (pdoc == NULL)
    {
        return ;
    }

    TiXmlDeclaration *pdeclaration = new TiXmlDeclaration("1.1.0", "UTF-8", "");
    if (pdeclaration == NULL)
    {
        return ;
    }
    pdoc->LinkEndChild(pdeclaration);

    //添加头结点,并添加到头结点
    TiXmlNode* pnodeANJUBAO = new TiXmlElement("ANJUBAO");
    pdoc->LinkEndChild(pnodeANJUBAO);
    //添加第二级节点
    TiXmlNode* pnodeSHOPALARM = new TiXmlElement("SHOP_ALARM");
    pnodeANJUBAO->LinkEndChild(pnodeSHOPALARM);

    //添加同一层次的4个节点,并设置属性和内容
    //注意类型转换不可直接强制转换,需要掉类的内部成员函数
    CreateNode(pnodeSHOPALARM, "IPC_ID", "123456", "type", "int");
    CreateNode(pnodeSHOPALARM, "IP_ADDR", "192.168.34.115", "type", "string");
    CreateNode(pnodeSHOPALARM, "PORT", "6666", "type", "int");
    CreateNode(pnodeSHOPALARM, "SENSOR", "AR0130", "type", "string");

    pdoc->SaveFile();

    return ;
}

TiXmlNode * selectChildNode(TiXmlNode * pNode, std::string nodeName, std::string nodeAttrName, std::string nodeAttrValue)
{
    if(pNode == NULL)
    {
        return NULL;
    }
    TiXmlNode * pSelectedNode = NULL;
    TiXmlNode * pChildNode = NULL;
    int t = pNode->Type();
    switch (t)
    {
    case TiXmlText::TINYXML_DOCUMENT:
    case TiXmlText::TINYXML_DECLARATION:
    case TiXmlText::TINYXML_TEXT:
    case TiXmlText::TINYXML_UNKNOWN:
    case TiXmlText::TINYXML_COMMENT:
        break;
    case TiXmlText::TINYXML_ELEMENT:
        if(pNode->Value() == nodeName)
        {
            if(!nodeAttrName.empty() && !nodeAttrValue.empty())
            {
                TiXmlElement * pElement = pNode->ToElement();
                TiXmlAttribute * pAttr = pElement->FirstAttribute();
                if(pAttr != NULL)
                {
                    do
                    {
                        if(pAttr->Name()==nodeAttrName&&pAttr->Value()== nodeAttrValue)
                        {
                            return pNode;
                        }
                    }
                    while((pAttr = pAttr->Next()) != NULL);
                }
            }
            else
            {
                return pNode;
            }

        }
        else
        {
            //循环访问它的每一个元素
            for(pChildNode=pNode->FirstChild();
                    pChildNode!=0;
                    pChildNode = pChildNode->NextSibling())
            {
                pSelectedNode = selectChildNode(pChildNode,nodeName,nodeAttrName,nodeAttrValue);
                if(pSelectedNode)
                {
                    return pSelectedNode;
                }
            }
        }

    default:
        break;
    }
    
    return NULL;
}

//通过根节点查找一个指定节点
TiXmlNode * SelectSingleNodeByRootEle(TiXmlElement* RootElement,const char *nodeName,const char *nodeAttrName,const char *nodeAttrValue)
{

    TiXmlNode * pNode  = NULL;
    TiXmlNode * pSelectNode = NULL;

    for(pNode=RootElement->FirstChildElement(); pNode; pNode=pNode->NextSiblingElement())
    {

        pSelectNode = selectChildNode(pNode,nodeName,nodeAttrName,nodeAttrValue);
        if(pSelectNode)
        {
            break;
        }
    }

    if(pSelectNode)
    {
        std::cout << "Find Node success!" << std::endl;
        std::cout << pSelectNode->Value() << std::endl;
        //std::cout << pSelectNode->ToElement()->GetText() << std::endl;
        return pSelectNode;
    }
    else
    {
        std::cout << "Find Node fail!" << std::endl;
        return NULL;
    }

}

//修改节点的属性和内容
void ModifyXmlFile(TiXmlElement* element, const char *NewText, const char *NewAttributeName,
                    const char *NewAttributeValue)
{
    element->Clear();
    if (NewAttributeName != NULL && NewAttributeValue != NULL)
    {
        element->SetAttribute(NewAttributeName, NewAttributeValue);
    }
    
    TiXmlText* pNEWtext = new TiXmlText(NewText);
    element->LinkEndChild(pNEWtext);

    return ;
}

//增加节点
TiXmlNode * AddElement(TiXmlElement *position, const char *ElementName, const char *ElementText,
                const char *AttributeName, const char *AttributeValue,
                const char *SecondAttributeName, const char *SecondAttributeValue)
{
    TiXmlElement* addNode = new TiXmlElement(ElementName);
    if(!addNode)
    {
        return NULL;
    }

    //增加节点的值
    if (ElementText != NULL)
    {
        TiXmlText* text = new TiXmlText(ElementText);
        addNode->LinkEndChild(text);
    }
    
    if (AttributeName != NULL && AttributeValue != NULL)
    {
        addNode->SetAttribute(AttributeName, AttributeValue);
    }
    if (SecondAttributeName != NULL && SecondAttributeValue != NULL)
    {
        addNode->SetAttribute(SecondAttributeName, SecondAttributeValue);
    }

    //增加到同一层的末端
    //position->Parent()->InsertEndChild(*addNode);
    //增加到节点的后面第一个位置
    //position->Parent()->InsertAfterChild(position, *addNode);


    //增加到子节点
    //position->InsertAfterChild(position, *addNode);
    TiXmlNode *ret = position->InsertEndChild(*addNode);

    return ret;
}

//删除指定节点
void DeleteElement(TiXmlNode *pNode)
{
    TiXmlNode *pParNode =  pNode->Parent();
    TiXmlElement* pParentEle = pParNode->ToElement();
    pParentEle->RemoveChild(pNode);

    return ;
}

//遍历同一层元素,并把所有内容输出到标准输出
void ReadXmlElement(TiXmlElement *pCurrentElement)
{
    while( pCurrentElement )
    {
        std::cout << "value: "<<pCurrentElement->Value()<<std::endl;;
        std::cout << "text: "<<pCurrentElement->GetText()<<std::endl;
        TiXmlAttribute *pAttribute = pCurrentElement->FirstAttribute();
        while(pAttribute)
        {
            std::cout<<"Attribute name: "<<pAttribute->Name()<<std::endl;
            std::cout<<"Attribute value: "<<pAttribute->Value()<<std::endl;
            pAttribute = pAttribute->Next();
        }
        pCurrentElement = pCurrentElement->NextSiblingElement();
    }
    return ;
}

const char *GetAttributeValue(TiXmlNode *pNode, const char *name)
{
    TiXmlElement *pElement = pNode->ToElement();
    TiXmlAttribute *pAttribute = pElement->FirstAttribute();
    const char *temp;
    
    while(pAttribute)
    {
        if (strcmp(pAttribute->Name(), name) == 0)
        {
           temp = pAttribute->Value();
           break;
        }else
        {
            pAttribute = pAttribute->Next();
        }      
    }

    return temp;
}

#include "parse_xml.h"
#include <cstdio>

int main(int argc, char *argv[])
{
    TiXmlDocument *pdoc = new TiXmlDocument;
    pdoc->LoadFile("./sensor.xml");

    TiXmlElement *pcur = pdoc->RootElement();
    std::cout<<"value: "<<pcur->Value()<<std::endl;

    AddElement(pcur, "sensor", "sensor", "mac", "12345678", "short_id", "1234");

    TiXmlNode *pNode =  SelectSingleNodeByRootEle(pcur, "sensor", "mac", "12345678");
    pNode =  SelectSingleNodeByRootEle(pcur, "sensor", "short_id", "1234");


    pdoc->SaveFile();
    delete pdoc;
    return 0;
}


TinyXml源码下载地址

http://download.csdn.net/detail/codeheng/8443511

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值