Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Reading Xml with dom



  • hi ,,,anybody pls help me to solve this problem

    i created xml file outside of the project ,,,i want to store account information into xml file for that i added this code

       QDomDocument   document;
             QDomElement    root =document.createElement("Contacts");
             document.appendChild(root);
    
              QDomElement node=document.createElement("contact");
              node.setAttribute("id",id);
              node.setAttribute("name",name);
              node.setAttribute("email",email);
              node.setAttribute("phonenumber",phone);
              node.setAttribute("quote",quote);
              node.setAttribute("country",country);
    
              root.appendChild(node);
              QFile file("E:/test/myxml.xml");
              if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
              {
    
              qDebug()<<"failed to open file for writting";
    
              }else{
              QTextStream stream(&file);
              stream<<document.toString();
              file.close();
    

    account information is saving into xml file but i hv to load(fetch) this information on mainwindow so i added this code but its not loading(fetching) !!!??

     APIManager::connectToServer();
    
           Datamanager::getInstance()->getEmail();
           Datamanager::getInstance()->getName();
           Datamanager::getInstance()->getPassword();
           Datamanager::getInstance()->getPhone();
           Datamanager::getInstance()->getQuote();
           Datamanager::getInstance()->getCountryList();
           Datamanager::getInstance()->getPhoneList();
           Datamanager::getInstance()->getAccountId();
    
           QDomDocument   document;
                   QFile file("E:/test/myxml.xml");
                   if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
                   {
                       qDebug()<<"Failed to open file";
    
               }else if(document.setContent(&file))
                       {
                           qDebug()<<"Failed to load document..";
                       }
    
                   file.close();
                   QDomElement root = document.firstChildElement();
    
                   ListElements(root,"Contacts","name");
                   QDomNodeList contacts=root.elementsByTagName("Contacts");
                   for(int i=0;i<contacts.count();i++)
                   {
                       QDomNode contactnode=contacts.at(i);
    
                       if(contactnode.isElement())
                       {
                           QDomElement contacts=contactnode.toElement();
                            qDebug()<< contacts.attribute("name");
                           ListElements(contacts,"Contact","name");
    
    
    
                       }
                   }
         }
    
    
       void MainWindow:: ListElements(QDomElement root, QString tagname, QString attributr)
       {
           QDomNodeList items=root.elementsByTagName(tagname);
           qDebug()<< "Total items = " <<items.count();
           for(int i=0;i<items.count();i++)
           {
               QDomNode itemnode=items.at(i);
    
               if(itemnode.isElement())
               {
                   QDomElement itemele=itemnode.toElement();
               }
           }
    


  • @Dimple said in Reading Xml with dom:

    root.elementsByTagName("Contacts");

    The children of root are have the name contact not Contacts


Log in to reply