Results 1 to 18 of 18

Thread: How to read a simple xml file.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    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
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    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
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    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
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    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 14:28.

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

    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
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 86 Times in 81 Posts

    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!!!

Similar Threads

  1. [Java] read and write a file
    By mickey in forum General Programming
    Replies: 3
    Last Post: 22nd June 2008, 11:43
  2. Replies: 1
    Last Post: 20th June 2008, 19: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, 09:47
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 16:21
  5. Replies: 13
    Last Post: 1st June 2006, 15: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
  •  
Qt is a trademark of The Qt Company.