QDomDocument使用

void CKsParseCategory::InitCategroy(QString szFileName, QList<CKsModuleCategory*> &listCategory)
{
    //clear current category list
    qDeleteAll(listCategory);
    listCategory.clear();

    //load file "module.xml"
    QFile file(szFileName);
    QDomDocument domDocument;

    QString errorStr;
    int errorLine;
    int errorColumn;

    //设置QFile
    if (!domDocument.setContent((QIODevice  *)&file, true, &errorStr, &errorLine, &errorColumn))
    {
        return;
    }
    QDomElement root = domDocument.documentElement();
    if (root.tagName() == "xml")
    {
        if (root.hasAttribute("version") && root.attribute("version") == "1.0")
        {
            QDomElement child = root.firstChildElement("category");
            while (!child.isNull())
            {
                parseCategoryElement(child, listCategory);
                child = child.nextSiblingElement("category");
            }
        }
    }
}
#include <QDebug>
void CKsParseCategory::parseCategoryElement(const QDomElement &element,
                                            QList<CKsModuleCategory *> &listCategory)
{
    CKsModuleCategory *kscategory;
    QString szCatename = element.attribute("name");
    QString szTypeName = element.attribute("type");
    QString szValue ,szDescrible;
    kscategory = new CKsModuleCategory(szCatename);

    if (kscategory == 0)
        return;


    szTypeName = szTypeName.trimmed();

    QDomElement child = element.firstChildElement();
    while (!child.isNull())
    {
        if (child.tagName() == "category")
        {
            parseCategoryElement(child,listCategory);
        }
        else
            if (child.tagName() == "module")
            {

            szValue = child.attribute("modulename");
            szDescrible = child.attribute("describe");
            CKsModule  *ksModule = new CKsModule(szValue, szTypeName, szDescrible);

            if(szCatename == "I/O")//输入输出模块按xml文件标注的类型进行设置
            {
                //    ksModule->SetModuleType(szCatename);
                if (szTypeName == QString("IO"))
                {
                    ksModule->SetType(CKsModule::E_IO);
                }
                else if (szTypeName == QString("SIGNLE") || szTypeName == QString(""))
                {
                    //没设置,按单道集处理
                    ksModule->SetType(CKsModule::E_SINGLE);
                }
                else if (szTypeName == QString("MORE"))
                {
                    ksModule->SetType(CKsModule::E_MORE);
                }
            }
            else//输入输出模块外的所有模块设为CKsModule::E_SINGLE类型(目的:添加暂停标记)
            {
                ksModule->SetType(CKsModule::E_SINGLE);
            }

            kscategory->AddModule(ksModule);

        }
        child = child.nextSiblingElement();
    }

    listCategory.append(kscategory);
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QDomDocument 是 Qt 框架中用于处理 XML 文档的类,以下是一个简单的使用示例: ```cpp #include <QDomDocument> #include <QFile> #include <QDebug> int main() { // 创建一个空的 QDomDocument 对象 QDomDocument doc; // 创建根元素 QDomElement root = doc.createElement("root"); doc.appendChild(root); // 创建子元素 QDomElement child1 = doc.createElement("child1"); child1.setAttribute("name", "Alice"); root.appendChild(child1); QDomElement child2 = doc.createElement("child2"); child2.setAttribute("name", "Bob"); root.appendChild(child2); // 保存到文件 QFile file("example.xml"); if (file.open(QIODevice::WriteOnly)) { QTextStream out(&file); doc.save(out, 4); file.close(); } // 从文件读取并解析 XML if (file.open(QIODevice::ReadOnly)) { if (doc.setContent(&file)) { QDomNodeList children = doc.documentElement().childNodes(); for (int i = 0; i < children.count(); i++) { QDomNode child = children.at(i); if (child.isElement()) { QDomElement element = child.toElement(); QString name = element.tagName(); QString value = element.attribute("name"); qDebug() << "Element: " << name << ", Attribute: " << value; } } } file.close(); } return 0; } ``` 这个示例中,我们首先创建了一个空的 QDomDocument 对象,然后创建了一个根元素和两个子元素,并将它们添加到文档中。接着,我们将文档保存到文件 example.xml 中,然后从文件中读取并解析 XML,输出每个元素的名称和属性值。输出结果如下: ``` Element: child1 , Attribute: Alice Element: child2 , Attribute: Bob ``` 以上就是 QDomDocument 的一个简单使用示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值