Results 1 to 18 of 18

Thread: How to read a simple xml file.

  1. #1
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Question How to read a simple xml file.

    Hi,
    First of all I must say "I am new to qt programming and also xml".
    I need to parse an xml file and read the file names. That's all I want to do.
    But I couldn't find any code example of reading simply an xml file. Anybody can help me please?

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read a simple xml file.

    To parse an XML file the simpler Qt way is to use QXmlStreamReader. See Qt Docs for examples.
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: How to read a simple xml file.

    What do you mean by Qt docs.
    I looked at Qt assistant and looked at QXmlStreamReader also. But these donot include a sample code.
    Could you give me a link please?

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read a simple xml file.

    Search "QXmlStream Bookmarks Example" in Qt Assistant
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: How to read a simple xml file.

    I looked at that example. Do I need to implement those xbelhandler classes?
    Do I need to write classses to read just a simple file??

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read a simple xml file.

    Just have a look at QDomDocument, QDomElement., and QDomElement::text(). and QDomDocument::setContent. This might be sufficient for u

    u can also refer to simple DOM Model in Qt Demos

  7. #7
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read a simple xml file.

    You can use directly QXmlStreamReader in your code.

    Simple code
    Qt Code:
    1. QFile file(fileName);
    2. file.open(QIODevice::ReadOnly);
    3.  
    4. QXmlStreamReader xml(&file);
    5. while (!xml.atEnd())
    6. {
    7. xml.readNext();
    8. ... // do processing
    9. }
    10. if (xml.hasError())
    11. {
    12. ... // do error handling
    13. }
    14. file.close();
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  8. #8
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: How to read a simple xml file.

    How can i reach the strings after reading?
    I want to see, what it read.
    So I will understand how this reader runs better.

  9. #9
    Join Date
    Jun 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to read a simple xml file.

    This code may help u............
    Qt Code:
    1. #ifndef CONTACT_H
    2. #define CONTACT_H
    3.  
    4. #include<QtCore/QString>
    5.  
    6. class QString;
    7.  
    8. class Contact
    9. {
    10. public:
    11. QString name,eMail,phone;
    12.  
    13. Contact( QString iName = "", QString iPhone = "", QString iEMail = "" );
    14. Contact( const QDomElement &e );
    15.  
    16. };
    17.  
    18. #endif
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //contact.cpp
    2.  
    3. #include"contact.h"
    4.  
    5. #include <qapplication.h>
    6. #include <QtXml/QDomNode>
    7.  
    8. #include <QtCore/QDir>
    9. #include <QtCore/QFile>
    10. #include <QtCore/QIODevice>
    11. #include <QDebug>
    12. #include <QtXml/QDomDocument>
    13.  
    14.  
    15.  
    16.  
    17. Contact::Contact( QString iName, QString iPhone, QString iEMail )
    18. {
    19. name = iName;
    20. phone = iPhone;
    21. eMail = iEMail;
    22. }
    23.  
    24. QDomElement ContactToNode(QDomDocument &d, const Contact &c )
    25. {
    26. QDomElement cn = d.createElement( "contact" );
    27.  
    28. cn.setAttribute( "NAME", c.name );
    29. cn.setAttribute( "PHONE", c.phone );
    30. cn.setAttribute( "EMAIL", c.eMail );
    31.  
    32. return cn;
    33. }
    34.  
    35.  
    36. int main( int argc, char **argv )
    37. {
    38.  
    39. QApplication a( argc, argv );
    40. QDomDocument doc("AdBookML");
    41. QDomElement root = doc.createElement( "adbook" );
    42. doc.appendChild(root);
    43.  
    44. Contact c;
    45.  
    46. c.name = "AAAA";
    47. c.eMail = "AAAA@gmail.com";
    48. c.phone = "+91-32233223";
    49.  
    50. root.appendChild(ContactToNode(doc,c));
    51.  
    52. QFile file("test.xml");
    53. if( !file.open( QIODevice::WriteOnly ) )
    54. return -1;
    55.  
    56. QTextStream ts( &file );
    57. ts << doc.toString();
    58.  
    59. file.close();
    60.  
    61.  
    62. c.name = "BBBBB";
    63. c.eMail = "BBBB@gmail.com";
    64. c.phone = "+91-3233443440";
    65.  
    66. root.appendChild( ContactToNode( doc, c ) );
    67. if( !file.open( QIODevice::WriteOnly ) )
    68. return -1;
    69.  
    70.  
    71. ts << doc.toString();
    72.  
    73. c.name = "CCCCC";
    74. c.eMail = "maassy@gmail.com";
    75. c.phone = "+91-98232388";
    76.  
    77. root.appendChild( ContactToNode( doc, c ) );
    78. if( !file.open( QIODevice::WriteOnly ) )
    79. return -1;
    80.  
    81.  
    82. ts << doc.toString();
    83.  
    84. file.close();
    85.  
    86. if( !file.open(QIODevice::ReadOnly ))
    87. return -1;
    88. if( !doc.setContent( &file ) )
    89. {
    90. file.close();
    91. return -2;
    92. }
    93. root = doc.documentElement();
    94. if( root.tagName() != "adbook" )
    95. return -3;
    96. QDomNode n = root.firstChild();
    97. while( !n.isNull() )
    98. {
    99. QDomElement e = n.toElement();
    100. if( !e.isNull() )
    101. {
    102. if( e.tagName() == "contact" )
    103. {
    104. Contact c ;
    105.  
    106. c.name = e.attribute( "NAME","" );
    107. c.phone = e.attribute( "PHONE","" );
    108. c.eMail = e.attribute( "EMAIL","" );
    109.  
    110. qDebug()<<c.name;
    111. qDebug()<<c.phone;
    112. qDebug()<<c.eMail;
    113.  
    114. }
    115. }
    116.  
    117. n = n.nextSibling();
    118. }
    119. return 0;
    120. }
    To copy to clipboard, switch view to plain text mode 
    Thanks

  10. #10
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: How to read a simple xml file.

    If I didnot understand wrong, the code above first create an xml file then reads it.
    Am I right?

    Edit: And also what is the difference between QDomElement and QDomNode?
    What are they beneficial For?
    Last edited by bod; 26th June 2008 at 13:28.

  11. #11
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: How to read a simple xml file.

    The QDomElement class represents one element in the DOM tree and the QDomNode class is the base class for all the nodes in a DOM tree.

  12. #12
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: How to read a simple xml file.

    What is base class?

  13. #13
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Post Re: How to read a simple xml file.

    Qt Code:
    1. QFile file("xmldenemev2.xbel");
    2. QXmlStreamReader xml(&file);
    3. QDomDocument doc("mydocument");
    4.  
    5. if (!file.open(QIODevice::ReadOnly))
    6. return;
    7.  
    8. if (!(doc.setContent(&file))) {
    9. ui.textEdit->setText("Fail in set Content");
    10. file.close();
    11. return;
    12. }
    13. file.close();
    14.  
    15. ui.textEdit->setText("SetContent is ok");
    16. // print out the element names of all elements that are direct children
    17. // of the outermost element.
    18. QDomElement docElem = doc.documentElement();
    19.  
    20. QDomNode n = docElem.firstChild();
    21. while(!n.isNull()) {
    22. QDomElement e = n.toElement(); // try to convert the node to an element.
    23. if(!e.isNull()) {
    24. ui.textEdit->setText("aGirdi");
    25. ui.textEdit->setText( e.tagName()); // the node really is an element.
    26. }
    27. n = n.nextSibling();
    28. }
    To copy to clipboard, switch view to plain text mode 

    Here is my code. But there is a mistake.It shouldn't enter the second "if". But it does.why "doc.setContent(&file)" returns false??

  14. #14
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read a simple xml file.

    As Qt Documentation says QDomDocument::setContent returns FALSE when the file isn't successfully parsed.

    Probably there is an error in XML file. Can you post it?
    A camel can go 14 days without drink,
    I can't!!!

  15. #15
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: How to read a simple xml file.

    Yes, I noticed that there is an error in the xml file. I have started to read, and playing with it. Thank you very much everybody who tries to help me

  16. #16
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: How to read a simple xml file.

    Hi, I started to read the xml file but now, I got an error.

    Here is my xml file.Im trying to read the file and compute the icons' address.


    Qt Code:
    1. <Icons>
    2. <backIcon>
    3. <belongsTo iconset="Vista">
    4. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\VistaIconSet</directory>
    5. <name>back.png</name>
    6. </belongsTo>
    7. <belongsTo iconset="Classic">
    8. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\ClassicIconSet</directory>
    9. <name>back.png</name>
    10. </belongsTo>
    11. </backIcon>
    12.  
    13. <nextIcon>
    14. <belongsTo iconset="Vista">
    15. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\VistaIconSet</directory>
    16. <name>forward.png</name>
    17. </belongsTo>
    18. <belongsTo iconset="Classic">
    19. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\ClassicIconSet</directory>
    20. <name>Next.png</name>
    21. </belongsTo>
    22. </nextIcon>
    23.  
    24. <stopIcon>
    25. <belongsTo iconset="Vista">
    26. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\VistaIconSet</directory>
    27. <name>stop.png</name>
    28. </belongsTo>
    29. <belongsTo iconset="Classic">
    30. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\ClassicIconSet</directory>
    31. <name>delete.png</name>
    32. </belongsTo>
    33. </stopIcon>
    34.  
    35. <saveIcon>
    36. <belongsTo iconset="Vista">
    37. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\VistaIconSet</directory>
    38. <name>save.png</name>
    39. </belongsTo>
    40. <belongsTo iconset="Classic">
    41. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\ClassicIconSet</directory>
    42. <name>save.png</name>
    43. </belongsTo>
    44. </saveIcon>
    45.  
    46. <printIcon>
    47. <belongsTo iconset="Vista">
    48. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\VistaIconSet</directory>
    49. <name>print.png</name>
    50. </belongsTo>
    51. <belongsTo iconset="Classic">
    52. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\ClassicIconSet</directory>
    53. <name>Print.png</name>
    54. </belongsTo>
    55. </printIcon>
    56.  
    57. <mailIcon>
    58. <belongsTo iconset="Vista">
    59. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\VistaIconSet</directory>
    60. <name>mail.png</name>
    61. </belongsTo>
    62. <belongsTo iconset="Classic">
    63. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\ClassicIconSet</directory>
    64. <name>Email.png</name>
    65. </belongsTo>
    66. </mailIcon>
    67.  
    68. <standartViewIcon>
    69. <belongsTo iconset="Vista">
    70. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\VistaIconSet</directory>
    71. <name>View.png</name>
    72. </belongsTo>
    73. <belongsTo iconset="Classic">
    74. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\ClassicIconSet</directory>
    75. <name>normalView.png</name>
    76. </belongsTo>
    77. </standartViewIcon>
    78.  
    79. <zoomInIcon>
    80. <belongsTo iconset="Vista">
    81. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\VistaIconSet</directory>
    82. <name>zoomin.png</name>
    83. </belongsTo>
    84. <belongsTo iconset="Classic">
    85. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\ClassicIconSet</directory>
    86. <name>zoomin.png</name>
    87. </belongsTo>
    88. </zoomInIcon>
    89.  
    90. <zoomOutIcon>
    91. <belongsTo iconset="Vista">
    92. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\VistaIconSet</directory>
    93. <name>zoomout.png</name>
    94. </belongsTo>
    95. <belongsTo iconset="Classic">
    96. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\ClassicIconSet</directory>
    97. <name>zoomout.png</name>
    98. </belongsTo>
    99. </zoomOutIcon>
    100.  
    101. <exitIcon>
    102. <belongsTo iconset="Vista">
    103. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\VistaIconSet</directory>
    104. <name>exit.png</name>
    105. </belongsTo>
    106. <belongsTo iconset="Classic">
    107. <directory>C:\Documents and Settings\tkaraman\Desktop\Assignment1\Assignment1\Images\ClassicIconSet</directory>
    108. <name>exit.png</name>
    109. </belongsTo>
    110. </exitIcon>
    111. </Icons>
    To copy to clipboard, switch view to plain text mode 
    Here is my code:
    Qt Code:
    1. for(QDomNode node=e.firstChild(); (!node.isNull()); node=node.nextSibling()){
    2. //QDomElement e5 = node.toElement();
    3. //ui.textEdit->append("\nXxXxxX" + e5.tagName());
    4. QDomNode locNode;
    5. for( locNode = node.firstChild(); (!locNode.isNull()); locNode = locNode.firstChild() ){
    6. QDomElement e2 = locNode.toElement();
    7. ui.textEdit->append("\n-_-_-" + e2.tagName());
    8. if((locNode.firstChild()).isNull() ){
    9. ui.textEdit->append("\nAAAAAAAAAAAAAAAAAAAAAAAA");
    10. QDomElement e3 = locNode.toElement();
    11. ui.textEdit->append("\n---??*?" + e3.tagName());
    12. QDomElement locElement = locNode.toElement();
    13. iconLocation =locElement.text();
    14. locNode = locNode.nextSibling();
    15. locElement = locNode.toElement();
    16. iconLocation = iconLocation + "\\" + locElement.text();
    17. ui.textEdit->append("\n+-+-+*" + iconLocation);
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    With the outer for loop I visit every icon. And With the Inner one, I'm trying to compute "directory\filename". However it does not enter the if part. When the reader reads the node <directory>, it should enter. Because that node has no child.Also it enters the inner loop 3 times, however it should enter 2?. Anybody can help me pls

  17. #17
    Join Date
    Jun 2008
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to read a simple xml file.

    In your xml file there are different tags related to icons .
    Do you want to read address /file name of each tag ?

    Plz explain more.....

  18. #18
    Join Date
    Jun 2008
    Posts
    32
    Qt products
    Qt4

    Default Re: How to read a simple xml file.

    there are 10 icons in my toolbar, backIcon, nextIcon etc..
    in directory tag, i store the information about where is the directory path.
    in name tag, i store the file name.
    So "directory"\"filename" gives me the complete path.(like "C:\Documents and Settings\mydocs\nextIcon.png".
    I dont know why the code above didnot work. but i did it with changing

    Qt Code:
    1. QDomElement e2 = locNode.toElement();
    2. ui.textEdit->append("\n-_-_-" + e2.tagName());
    3. if( e2.tagName() == "directory" ){
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. [Java] read and write a file
    By mickey in forum General Programming
    Replies: 3
    Last Post: 22nd June 2008, 10:43
  2. Replies: 1
    Last Post: 20th June 2008, 18:43
  3. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 08:47
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Replies: 13
    Last Post: 1st June 2006, 14:01

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.