QDomDocument question

Ingo Foerster
Ingo Foerster used Ask the Experts™
on
Hello,
I want to read some values from a XML file by QDomDocument class.  But I always fail. So I hope someone know how to read atributes with QDomDocument . My XML file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xbel>
<xbel name="project" version="1.0" date="Di Jul 21 2015" size="0" item_count="0">
  <settings/>
  <disk projecttype="1" name="Disk[New]"  arranger=""/>
</xbel>

Open in new window


I want to read the attribute projecttype that can stay in front or behind name. (Not my idea).

Any help will be welcome.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
evilrixSenior Software Engineer (Avast)

Commented:
>> But I always fail.
Example code with details of the errors you get would be useful.
Programmer
Commented:
Hi,
I have found some code in the internet and have rewritten to my needs. Works like a charme. Here is the code I use:

  QDomDocument doc;
  doc.setContent(&domFile);

  int nDiskType = -1;

  QDomNodeList diskList = doc.elementsByTagName("disk");

  for (int i = 0; i < diskList.count(); i++)
  {
      if (i < diskList.count())
      {
          QDomElement node = diskList.at(i).toElement();
          if (node.hasAttribute("projecttype"))
          {
              nDiskType = node.attribute("projecttype").toInt();
              break;
          }
      }
  }


  domFile.close();

Open in new window

Ingo FoersterProgrammer

Author

Commented:
Own solution. Published to share with people here.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial