Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reading Xml with dom

Reading Xml with dom

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 381 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dimple
    wrote on last edited by VRonin
    #1

    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();
               }
           }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @Dimple said in Reading Xml with dom:

      root.elementsByTagName("Contacts");

      The children of root are have the name contact not Contacts

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved