Results 1 to 3 of 3

Thread: Access elements of the xml-file

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Access elements of the xml-file

    Hi, everyone!

    I read the xml-file:

    Qt Code:
    1. <Matrix>
    2. <Description>
    3. <rows>3</rows>
    4. <columns>4</columns>
    5. </Description>
    6. <row1>
    7. <cell>23.7</cell>
    8. <cell>2.7</cell>
    9. <cell>3.7</cell>
    10. <cell>23.9</cell>
    11. </row1>
    12. <row2>
    13. <cell>13.0</cell>
    14. <cell>2.7</cell>
    15. <cell>3.7</cell>
    16. <cell>2</cell>
    17. </row2>
    18. <row3>
    19. <cell>2.5</cell>
    20. <cell>2.7</cell>
    21. <cell>3.7</cell>
    22. <cell>23.9</cell>
    23. </row3>
    24. </Matrix>
    To copy to clipboard, switch view to plain text mode 

    I want to write to the console these names of tags 'row1' and 'row2':

    Qt Code:
    1. qDebug() << rowElement1.tagName();
    2. qDebug() << rowElement2.tagName();
    To copy to clipboard, switch view to plain text mode 

    I expect to see:
    "row1"
    "row2"
    But I see:
    "row1"
    "row1"
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include <QFile>
    4. #include <QTextStream>
    5.  
    6. #include <QDomDocument>
    7. #include <QDomElement>
    8. #include <QDomText>
    9.  
    10. #include <QMessageBox>
    11.  
    12. #include <QDebug>
    13. #include <QObject>
    14.  
    15. int main(int argc, char **argv)
    16. {
    17. QApplication app(argc, argv);
    18.  
    19. QFile file( "matrix.xml" );
    20. if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
    21. {
    22. QMessageBox::critical(NULL, QObject::tr("Error"), QObject::tr("Failed to open file for reading"));
    23. return 0;
    24. }
    25.  
    26. QDomDocument document;
    27. if( !document.setContent( &file ) )
    28. {
    29. QMessageBox::critical(NULL, QObject::tr("Error"), QObject::tr("Failed to parse the file into a DOM tree"));
    30. file.close();
    31. return 0;
    32. }
    33.  
    34. file.close();
    35.  
    36. QDomElement matrixElement = document.documentElement();
    37.  
    38. QDomNode descriptionNode = matrixElement.firstChild();
    39.  
    40. QDomElement rowElement1 = descriptionNode.nextSiblingElement();
    41. qDebug() << rowElement1.tagName();
    42.  
    43. QDomElement rowElement2 = descriptionNode.nextSiblingElement();
    44. qDebug() << rowElement2.tagName();
    45.  
    46. app.exec();
    47. }
    To copy to clipboard, switch view to plain text mode 

    Thanks!

    Ivan

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Access elements of the xml-file

    Qt Code:
    1. QDomElement rowElement2 = rowElement1.nextSiblingElement();
    2. qDebug() << rowElement2.tagName();
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lykurg for this useful post:

    8Observer8 (8th January 2013)

  4. #3
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Access elements of the xml-file

    Thank you very much!

Similar Threads

  1. SVG: access coordinates of elements
    By bmpix in forum Qt Programming
    Replies: 5
    Last Post: 31st August 2012, 23:29
  2. Access to js imported file in another qml file
    By alizadeh91 in forum Qt Quick
    Replies: 5
    Last Post: 16th July 2012, 15:21
  3. Accessing elements of a svg file
    By Hogwarts in forum Qt Programming
    Replies: 2
    Last Post: 22nd February 2011, 14:00
  4. Replies: 4
    Last Post: 6th August 2009, 06:12
  5. How to create a access file .mdb
    By aiikcmo in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 10: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.