Results 1 to 10 of 10

Thread: Problems reading XML with QDomDocument

  1. #1
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problems reading XML with QDomDocument

    Hi,

    I am trying to read configuration with QDomDocument. I managed to get writting configuration to work. When reading I get the settings but don't get any phone numbers.

    This is an example of what I might read (there can be between 0 and 999 phone numbers):

    Qt Code:
    1. <!--Main Settings-->
    2. <settings AfterHourOffset="100" NumPerButton="1" MaxNumToDial="1" />
    3. <!--Phone Book List-->
    4. <phone location="0" value="911" />
    5. <phone location="1" value="33782134" />
    6. <phone location="2" value="000" />
    7. <phone location="3" value="ABCD" />
    8. <phone location="8" value="13005696" />
    9. <phone location="990" value="38441104" />
    10. <phone location="999" value="131333" />
    To copy to clipboard, switch view to plain text mode 

    Here's my current attempt:

    Qt Code:
    1. QFile file("fred.xml");
    2.  
    3. if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    4.  
    5.  
    6. d.setContent(&file);
    7.  
    8. QDomNode n = d.firstChild();
    9.  
    10. ClearPhoneNumbers(); // Clear phone table.
    11.  
    12. while(!n.isNull()) {
    13.  
    14. if(n.isElement()) {
    15.  
    16. QDomElement e = n.toElement();
    17. QString name = e.tagName();
    18.  
    19. if(name == "settings") {
    20.  
    21. ui->Edit1->setText(e.attribute("AfterHourOffset",""));
    22. ui->Edit2->setText(e.attribute("MaxNumToDial",""));
    23. ui->Edit3->setText(e.attribute("NumPerButton",""));
    24.  
    25. } else if(name == "phone") {
    26.  
    27. QString location = e.attribute("location","");
    28. QString value = e.attribute("value","");
    29.  
    30. AddPhoneBook(location, value); // Add to phone table.
    31. }
    32. }
    33. n = n.nextSibling();
    34. }
    35. file.close();
    36. }
    To copy to clipboard, switch view to plain text mode 

    See whats wrong?

    Thanks
    Brendan

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems reading XML with QDomDocument

    your xml format is not corrected. (you xml doesn't contain root element).
    you should have xml like this
    Qt Code:
    1. <phones>
    2. <!--Main Settings-->
    3. <settings AfterHourOffset="100" NumPerButton="1" MaxNumToDial="1" />
    4. <!--Phone Book List-->
    5. <phone location="0" value="911" />
    6. <phone location="1" value="33782134" />
    7. <phone location="2" value="000" />
    8. <phone location="3" value="ABCD" />
    9. <phone location="8" value="13005696" />
    10. <phone location="990" value="38441104" />
    11. <phone location="999" value="131333" />
    12. </phones>
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems reading XML with QDomDocument

    I think you should use something as:
    <?xml version='1.0' encoding='windows-1251'?>
    <root>
    <data1></data1>
    ....
    </root>
    east or west home is best

  4. #4
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems reading XML with QDomDocument

    Thanks for replies,

    I tried both suggestions. Now it only collects the root element. Then after NextSibling is called it breaks out of the while loop. So nothing is collected. I must be missing something.

  5. #5
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems reading XML with QDomDocument

    I use this code to read xml.

    Qt Code:
    1. bool CSKHttp::GetDataFromXml(CSKRequest* req)
    2. {
    3. QDomDocument domDoc;
    4. QString errorString;
    5. int errorLine;
    6. int errorColumn;
    7. req->m_buffer.open(QIODevice::ReadOnly);
    8. QByteArray byteArray(req->m_buffer.readAll());
    9. req->m_buffer.close();
    10. if(!(domDoc.setContent(byteArray,false,&errorString,&errorLine,&errorColumn))) {
    11. mocW("rq")<<"xml document error <MESS:"<<errorString<<"><LINE"
    12. <<errorLine<<"><COLUMN"<<errorColumn<<">"<<endl;
    13. mocW("rq")<<"<ANSWER:"<<byteArray<<">"<<endl;
    14. return false;
    15. }
    16. QDomElement domElement = domDoc.documentElement();
    17. byteArray.replace("\n"," ");
    18. mocD("rq")<<"<ANSWER:"<<byteArray<<">"<<endl;
    19. TraverseXmlNode(domElement,req);
    20. return true;
    21. }
    22.  
    23. void CSKHttp::TraverseXmlNode(const QDomNode& node, CSKRequest* req)
    24. {
    25. QDomNode domNode = node.firstChild();
    26. QDomElement domElement;
    27. while(!(domNode.isNull())) {
    28. if(domNode.isElement()) {
    29. domElement = domNode.toElement();
    30. if(!(domElement.isNull()))
    31. req->Data(domElement.tagName(),domElement.text());
    32. }
    33. TraverseXmlNode(domNode,req);
    34. domNode = domNode.nextSibling();
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 
    may be useful for you.
    east or west home is best

  6. The following user says thank you to kwisp for this useful post:

    grantbj74 (27th August 2009)

  7. #6
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems reading XML with QDomDocument

    You get the first child, which would be "phones". Then you try to get its siblings but it doesn't have any. You need to get the children of phones. So you can do:

    Qt Code:
    1. d.setContent(&file);
    2.  
    3. QDomNode n = d.firstChild();
    4. if(!n.isNull() || !n.hasChildNodes())
    5. return;
    6.  
    7. n = n.firstChild();
    8.  
    9. ClearPhoneNumbers(); // Clear phone table.
    10.  
    11. while(!n.isNull()) {
    12. ...
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to victor.fernandez for this useful post:

    grantbj74 (27th August 2009)

  9. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problems reading XML with QDomDocument

    I wrote this in a browser, but I'm sure that this code works fine.
    Qt Code:
    1. QFile file("phones.xml");
    2. if (!file.open(QIODevice::ReadOnly))
    3. return;
    4. if (!doc.setContent(&file)) {
    5. file.close();
    6. return;
    7. }
    8. file.close();
    9.  
    10. QDomElement docElem = doc.documentElement();
    11.  
    12. QDomNode n = docElem.firstChild();
    13. while(!n.isNull()) {
    14. QDomElement e = n.toElement();
    15. if(!e.isNull()) {
    16. if (e.tagName() == "phone") {
    17. qDebug() << "location" << e.attribute("location");
    18. qDebug() << "value" << e.attribute("value");
    19. } else if (e.tagName() == "settings") {
    20. qDebug() << "AfterHourOffset" << e.attribute("AfterHourOffset");
    21. qDebug() << "NumPerButton" << e.attribute("NumPerButton");
    22. qDebug() << "MaxNumToDial" << e.attribute("MaxNumToDial");
    23. }
    24. }
    25. n = n.nextSibling();
    26. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. The following user says thank you to spirit for this useful post:

    grantbj74 (27th August 2009)

  11. #8
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Problems reading XML with QDomDocument

    Thanks poeple.

    Spirits version worked for me.

    I ended up making mine a combination of Spirit and victor.fernandez ideas.

    I'm a bit scared to do recursive programming unless its required.

  12. #9
    Join Date
    Dec 2008
    Location
    TaganrogNativelandChehov,Russia
    Posts
    64
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems reading XML with QDomDocument

    Quote Originally Posted by grantbj74 View Post
    Thanks poeple.
    ....
    I'm a bit scared to do recursive programming unless its required.
    don`t be afraid it`s realy easy.
    east or west home is best

  13. #10
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems reading XML with QDomDocument

    I'm a bit scared to do recursive programming unless its required.
    There is some risk of making a mistake and entering an infinite recursion but no more than there is with a plain for(). Being a bit careful, you should not be afraid of it. Also keep in mind that by traversing a QDomDocument you won't have any problem because it's a plain tree without loop backs.

Similar Threads

  1. Mac OSX OpenGL problems
    By tksharpless in forum Qt Programming
    Replies: 0
    Last Post: 23rd March 2009, 17:27
  2. QDomDocument inside other QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2008, 15:27
  3. How to Compile VTKDesigner2 with Qt?
    By alfredoaal in forum Newbie
    Replies: 0
    Last Post: 5th September 2008, 05:34
  4. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  5. Adding whitespace in QDomDocument
    By jakamph in forum Newbie
    Replies: 4
    Last Post: 6th February 2006, 22:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.