TinyXml库的基本操作

#ifndef _PARSE_XML_H_
#define _PARSE_XML_H_

#include "tinyxml.h"
#include "tinystr.h"
#include 

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: "<Value()<GetText()<FirstAttribute();
        while(pAttribute)
        {
            std::cout<<"Attribute name: "<Name()<Value()<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 

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

    TiXmlElement *pcur = pdoc->RootElement();
    std::cout<<"value: "<Value()<SaveFile();
    delete pdoc;
    return 0;
}


TinyXml源码下载地址

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

你可能感兴趣的:(嵌入式开发,Linux)