Results 1 to 3 of 3

Thread: parse xml

  1. #1
    Join Date
    Jun 2007
    Location
    italy
    Posts
    126
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default parse xml

    is there a generic way to parse all the node of a xml document, and print out the element name its attributes (name="value") and its values ?
    For generic way I mean something that not require me to loop many time based on annidation lavel, since in this case it means I have to know the xml specific annidation level..

    Thank you
    roby

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: parse xml

    You can write a recursive parser using QXmlStreamReader or QDomDocument. You can start with this:
    Qt Code:
    1. void displayElement(const QDomElement &elem, int indent = 0){
    2. QString out = QString(indent, ' ')+elem.name()+" ";
    3. out += /* parse attributes here */
    4. qDebug() << out;
    5. for(QDomElement subElem = elem.firstChildElement(); !elem.isNull(); subElem = subElem.nextSiblingElement()){
    6. displayElement(subElem, indent+4);
    7. }
    8. }
    9.  
    10. doc.setContent(...);
    11. displayElement(doc.documentElement());
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    rmagro (2nd July 2009)

  4. #3
    Join Date
    Sep 2008
    Location
    Germany
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: parse xml

    This is the QXmlStreamReader version. This example will create an output like the following:

    -----start----
    [ElementName]
    [Attribute = Value, ...]
    [ElementText if existing]
    -----end-----

    The bool flag is necessary since reader.readElementText() raises an error if there is no element text. The stream reader version should have very good performance.

    Qt Code:
    1. QFile sourceFile("source.xml");
    2. if( !sourceFile.open( QIODevice::ReadOnly ) )
    3. return;
    4.  
    5. QFile targetFile("output.txt");
    6. if( !targetFile.open( QIODevice::WriteOnly ) )
    7. return;
    8.  
    9. QXmlStreamReader reader(&sourceFile);
    10.  
    11. QTextStream writer(&targetFile);
    12.  
    13.  
    14. bool lastElementHadText = true;
    15. while( !reader.atEnd() ) {
    16.  
    17. reader.readNext();
    18.  
    19. if( reader.isStartElement() ) {
    20. if( !lastElementHadText )
    21. writer << "-----end-----" <<endl;
    22.  
    23. writer << "-----start-----" << endl;
    24. writer << reader.name().toString() << endl;
    25. foreach( QXmlStreamAttribute attribute, reader.attributes() ) {
    26. writer << attribute.name().toString() << "=" << attribute.value().toString() << ", ";
    27. }
    28. if( reader.attributes().count() > 0 )
    29. writer << endl;
    30. }
    31.  
    32. if( reader.isCharacters() && !reader.isWhitespace() ) {
    33. lastElementHadText = true;
    34. writer << reader.text().toString() << endl;
    35. writer << "-----end-----" <<endl;
    36. }
    37.  
    38. }
    39.  
    40. writer.flush();
    41. targetFile.close();
    42. sourceFile.close();
    To copy to clipboard, switch view to plain text mode 


    greetings

  5. The following user says thank you to smeck for this useful post:

    rmagro (20th October 2009)

Similar Threads

  1. how QXmlStreamReader parse QString
    By SamSong in forum Qt Programming
    Replies: 2
    Last Post: 6th November 2009, 14:56
  2. Best way to load and parse an HTML file ??
    By tuthmosis in forum Qt Programming
    Replies: 8
    Last Post: 23rd August 2008, 11:06
  3. Parse QTextBlock from QTextDocument problem
    By patrik08 in forum Qt Programming
    Replies: 0
    Last Post: 5th April 2008, 08:26
  4. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 12:19
  5. How to parse this XML file ?
    By probine in forum Qt Programming
    Replies: 7
    Last Post: 4th April 2006, 09:05

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.