使用tinyxml解析和修改XML文件

首先要清楚XML文件包含哪些元素:

他是由元素、文本或者两者混合物组成。元素可以拥有属性,元素是指从开始标签到结束标签的部分。

XML 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<books>
          <book id="1001">
           
                <name>面纱</name>
                <info>请记住我,虽然再见必须说</info>
          </book>
           <book id="1002">
                <name>人生第一次</name>
                <info>愿他们、我们的一生平淡而有意义</info>
          </book>
</books> 

开始操作

1.在网上下载到tinnyxml的源码

2.导入tinyxml的头文件

复制代码
#include "./TinyXML/tinyxml.h"

解析方法:

cpp 复制代码
QString XMLreadwrite::parseIPAddress() const
{
    QList<QString> nodeList;
    nodeList.append("net");
    nodeList.append("fix_ip");
    QString attrText = "ip";

    TiXmlNode *pNode = (TiXmlNode*)FindTextNode(nodeList);
    QString data = ParseNodeData(pNode, attrText);

    return data;
}

修改方法:

cpp 复制代码
void XMLreadwrite::ModifyNodeData(TiXmlNode *pNode, QString text, QString data) const
{
    for(pNode = pNode->FirstChild();
        pNode;
        pNode = pNode->NextSibling())
    {
        if(QString(QLatin1String(pNode->Value())) == text)
        {
            qDebug()<<"......."<<text<<data;
            const char cData[1024] = {0};
            memcpy((void*)cData,data.toStdString().c_str(),data.size());
            //首先清除所有文本
            pNode->Clear();
            //然后插入文本
            TiXmlText  *pValue = new TiXmlText(cData);
            pNode->LinkEndChild(pValue);
            qDebug()<<"Modified successfully"<<QString(QLatin1String(pNode->ToElement()->GetText()));
        }
    }
}

解析例子:

cpp 复制代码
QString XMLreadwrite::parseCodeFormat() const
{
    QList<QString> nodeList;
    nodeList.append("param_video");
    QString attrText = "attr_format";

    TiXmlNode *pNode = (TiXmlNode*)FindTextNode(nodeList);
    QString nodeAttr = QString(QLatin1String(pNode->ToElement()->Attribute("name")));
    QString data = "";
    if(nodeAttr == "Visible")
    {
        data = ParseNodeData(pNode, attrText);
    }

    return data;
}

修改例子:

cpp 复制代码
void XMLreadwrite::modifyCodeFormat(QString data)
{
    QList<QString> nodeList;
    nodeList.append("param_video");
    QString attrText = "attr_format";

    TiXmlNode *pNode = (TiXmlNode*)FindTextNode(nodeList);
    QString nodeAttr = QString(QLatin1String(pNode->ToElement()->Attribute("name")));
    if(nodeAttr == "Visible")
    {
        ModifyNodeData(pNode, attrText, data);
    }
}
相关推荐
筝筝ba4 天前
OpenBMC 自动化测试中的多进程陷阱:从 Execute Process 返回异常到 output.xml 损坏问题分析
xml·linux·运维·服务器·网络
spencer_tseng9 天前
[j2cache ehcache.xml]/tmp/.ehcache-diskstore.lock (Permission denied)
xml·linux·python·ehcache·[j2cache
DevOpenClub10 天前
PDF 多格式解析如何避免混用输出:TEXT、HTML、XML 与 TAG 数据契约
xml·前端·数据库·pdf·html
Java小白笔记10 天前
MyBatis XML Mapper 标签与 CRUD 原理实战
xml·数据库·mybatis
风萧萧199911 天前
JAVA :JSONObject转换为XML
xml·java·python
马优晨13 天前
pom.xml 中 jquery webjars 依赖解释
xml·前端·jquery·jquery webjars·webjars依赖
楚识科技14 天前
定制 OCR 服务:非标证件与表格的 XML 字段映射实践
xml·ocr
光泽雨15 天前
JSON /XML转换
xml·json
我命由我1234516 天前
UI 设计 7 种排列方式
xml·学习·ui·html·产品运营·产品经理·学习方法