【问题标题】:XML file load QT not workingXML文件加载QT不起作用
【发布时间】:2015-07-01 15:00:46
【问题描述】:

这个 xml 读取根本不起作用,我不明白为什么。我不确定它是否正确创建了用户对象,但无论如何我无法在数据库加载后登录任何帐户。

每当我在不关闭应用程序的情况下注册用户帐户时,我都可以登录,所以我想这不是登录问题。 XML 保存工作正常。

我的数据库是 SmartUtente* 对象的列表。 SmartUtente 对象只是一个用户*。

void Database::load(string p){
cout<<"Caricamento in corso..."<<endl;
try{
    if(p.empty()) return;
    if(!p.empty()) clean();
    QDomDocument doc("dbLinkedin");
    QFile file(QString::fromStdString(path));
    if (!file.open(QIODevice::ReadOnly | QIODevice::WriteOnly)){
        cout<<"Errore: permessi insufficienti \n";
        throw Eccezioni::BadPermission();
    }
    int n = 0;
    El users = doc.firstChildElement("users").firstChildElement("user");
    while(!users.isNull()){
        El pointer;
        Utente * s;
        if(users.attributeNode("type").value() == "basic"){
            s= new UtenteBasic();
        }
        else if(users.attributeNode("type").value() == "business"){
            s= new UtenteBusiness();
        }
        else if(users.attributeNode("type").value() == "executive"){
            s= new UtenteBusiness();
        }
        s->setName((users.firstChildElement("name").text()).toStdString());
        s->setSurname((users.firstChildElement("surname").text()).toStdString());
        s->setUsername((users.firstChildElement("username").text()).toStdString());
        s->setPassword((users.firstChildElement("password").text()).toStdString());
        s->setSesso((users.firstChildElement("genre").text()).toStdString());
        s->setCity((users.firstChildElement("city").text()).toStdString());
        s->setEmail((users.firstChildElement("email").text()).toStdString());
        pointer= users.firstChildElement("qualifications").firstChildElement("qualific");
        while(!pointer.isNull()){
            s->setQualification((pointer.text()).toStdString());
            pointer= pointer.nextSiblingElement("qualification");
        }
        s->setBirth((users.firstChildElement("birth").text()).toStdString());
        pointer= users.firstChildElement("contacts").firstChildElement("contact");
        while(!pointer.isNull()){
            s->setRete((pointer.text()).toStdString());
            pointer= pointer.nextSiblingElement("contact");
        }
        SmartUtente* su= new SmartUtente(s);
        db.push_back(su);
        users= users.nextSiblingElement("user");
        n++;
    }
    cout<<"Termine caricamento"<<endl;
    setPath(p);
    cout<<"Path settato"<<endl;
    file.close();
}
catch(Eccezioni::BadPermission){
    QMessageBox::information(0,"Caricamento non riuscito","E' possibile che non si disponga più dei permessi necessari per leggere il database");
    return;
}

}

【问题讨论】:

  • 只是为了澄清,我建议将其也添加到问题中:您创建了这个 SmartUtente,我相信它是某种 Xml 对象,然后您可以使用它登录到某个地方对吗?登录无法正常工作,但您认为问题出在这部分代码中。正确吗?
  • SmarUtente 只是一个 Utente* 而我的数据库是一个列表
  • 两件事: 1. 您正在创建和使用 QDomDocument 而不向其中加载任何 XML 内容。 2. QtXML 不再维护。如果我是你,我不会依赖它。
  • XML文件引用QFile文件(QString::fromStdString(path));路径是 ./data.xml 无论如何这是一个大学项目,所以很不幸这不是个人选择。

标签: xml qt load


【解决方案1】:

解决了。

缺少 doc.setContent(&file);

【讨论】:

    猜你喜欢
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多