Results 1 to 5 of 5

Thread: QDomDocument and reading XML

  1. #1
    Join Date
    Sep 2009
    Posts
    49
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QDomDocument and reading XML

    Hi,

    First time using QT and XML. Here is my simple XML file.
    Qt Code:
    1. <?xml version="1.0"?>
    2.  
    3. <Node nodetype="atomic"
    4. header="atomic.h">
    5.  
    6. <Input name="inNumber" type="color" value="0.2,0.3,0.5"/>
    7. <Output name="outNumber" type="float" value="0.5"/>
    8.  
    9. </Node>
    To copy to clipboard, switch view to plain text mode 

    and here is the QT code:
    Qt Code:
    1. QFile file("atomic.xml");
    2. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    3. return 0;
    4.  
    5. if (!doc.setContent(&file)) {
    6. file.close();
    7. return 0;
    8. }
    9. file.close();
    10.  
    11. QDomNode n = doc.firstChild();
    12.  
    13. while(!n.isNull()) {
    14.  
    15. if(n.isElement()) {
    16.  
    17. QDomElement e = n.toElement();
    18. QString name = e.tagName();
    19.  
    20. qDebug() << name;
    21.  
    22. if(name == "Node") {
    23.  
    24. QString nodeType = e.attribute("nodetype","");
    25. QString header = e.attribute("header","");
    26. qDebug() << nodeType << header;
    27.  
    28. } else if(name == "Input") {
    29.  
    30. QString name1 = e.attribute("name","");
    31. QString type1 = e.attribute("type","");
    32. QString value1 = e.attribute("value","");
    33.  
    34. qDebug() << name1 << type1 << value1;
    35. }
    36. }
    37. n = n.nextSibling();
    38. }
    To copy to clipboard, switch view to plain text mode 

    Problem 1
    The code is not entering in condition:
    Qt Code:
    1. else if(name == "Input") {
    To copy to clipboard, switch view to plain text mode 

    Cheers

    Prashant

  2. #2
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDomDocument and reading XML

    Hi,

    The tag "Input" is child the node "Node". Is its problem.

    Marcelo E. Geyer

  3. #3
    Join Date
    Sep 2009
    Posts
    49
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDomDocument and reading XML

    Not able to get the child nodes of "Node" this way:
    Qt Code:
    1. for(signed i(0); i<n.childNodes().count();i++){
    2.  
    3. QDomNode childNode = n.childNodes().item(i);
    4. QDomElement e = childNode.toElement();
    5. QString name = e.tagName();
    6. if(name == "Input") {
    7.  
    8. QString name1 = e.attribute("name","");
    9. QString type1 = e.attribute("type","");
    10. QString value1 = e.attribute("value","");
    11.  
    12. qDebug() << name1 << type1 << value1;
    13. }
    To copy to clipboard, switch view to plain text mode 

    It's because:
    Qt Code:
    1. n.hasChildNodes(); // = 0
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Posts
    49
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDomDocument and reading XML

    I didn't find any solution so far. May be some one point out the solution.

    Cheers

    Prashant

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDomDocument and reading XML

    chage this:
    Qt Code:
    1. QDomNode n = doc.firstChild();
    To copy to clipboard, switch view to plain text mode 
    to this:
    Qt Code:
    1. QDomElement e = doc.documentElement(); // now e contains "Node" - it's a document element - means root element
    To copy to clipboard, switch view to plain text mode 
    and use QDomElements if you want to get only elements. You can iterate through "Node" childs like this:
    Qt Code:
    1. QDomElement child = e.firstChildElement();
    2. while (!child.isNull()) {
    3. // do sth with child
    4. // it should be "Input" in first iteration and "Output" in second one
    5. child = child.nextSiblingElement(); // move 'child' to be the next sibling element
    6. }
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

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

    prashant (20th September 2009)

Similar Threads

  1. Problems reading XML with QDomDocument
    By grantbj74 in forum Qt Programming
    Replies: 9
    Last Post: 27th August 2009, 08:14
  2. XML File reading issue
    By yogesh in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2009, 14:46
  3. help in reading XML file
    By cshiva_in in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2008, 13:55

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.