Results 1 to 4 of 4

Thread: reading xml file

  1. #1
    Join Date
    Feb 2008
    Posts
    102
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question reading xml file

    I have an xml file like this:
    Qt Code:
    1. <start_header>
    2. "text1"
    3. "text2"
    4. </start_header>
    5. <option>
    6. "text3"
    7. "text4"
    8. </option>
    9.  
    10. ...and so on
    To copy to clipboard, switch view to plain text mode 

    I'd like to extract just the Textelement between each pair of tag and write it
    in a file.txt

    File.txt content:
    "text1"
    "text2"
    "text3"
    "text4"

    I use the special class QXmlStreamReader and i try to obtain this behaviour with this code:
    Qt Code:
    1. QFile *file=new QFile("source.xml")
    2. QFile *f=new QFile("output.txt");
    3. if(!f->open(QIODevice::ReadWrite))
    4. std::cout<<"f problem"<<std::endl;
    5. if(!file->open(QIODevice::ReadWrite))
    6. std::cout<<"f problem"<<std::endl;
    7.  
    8. QTextStream out(f);
    9. QXmlStreamReader xml(file);
    10.  
    11. while(!xml.atEnd()){
    12. if (xml.readNext() == QXmlStreamReader::StartElement)
    13. out<<xml.readElementText()<<"\n";
    14. }
    15. f->close();
    16. file->close();
    To copy to clipboard, switch view to plain text mode 

    The output file wasn't writed......
    What's wrong with this code?
    Is there an other method, to obtain the behaviuor I looked for?

  2. #2
    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: reading xml file

    May be the xml data is not valid.
    How can text1 , text 2 be in one tag ??
    also try to debug and see what values u are getting for readNextElement.

  3. #3
    Join Date
    Feb 2008
    Posts
    102
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: reading xml file

    I create the xml file from a DOM tree, using the function save.

    The element inside tags are QDomElement, while the text are QDomText......

  4. #4
    Join Date
    Mar 2006
    Posts
    140
    Thanks
    8
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: reading xml file

    The only thing I see wrong with that data is there's no root element. Try:
    Qt Code:
    1. <root>
    2. <start_header>
    3. "text1"
    4. "text2"
    5. </start_header>
    6. <option>
    7. "text3"
    8. "text4"
    9. </option>
    10. </root>
    To copy to clipboard, switch view to plain text mode 

    Keep in mind though that "text1" "text2" are one element's worth of data. It will actually be interpreted as:
    Qt Code:
    1. "text1" "text2"
    To copy to clipboard, switch view to plain text mode 
    The XML standard treats each line of text as separated by one space, even you indent the lines one space will be used. I'm not sure what would happen to the " characters, maybe they're illegal as well. Everything is treated as text in the xml file itself so you don't need to qualify strings with double quotes.
    If one of these elements need to be treated as an integer, you're schema can state the type or you just read the line, then cast / convert it to the type you need.
    Last edited by stevey; 5th May 2008 at 02:57.

Similar Threads

  1. help in reading XML file
    By cshiva_in in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2008, 13:55
  2. QTextStream loses position while reading file
    By bjh in forum Qt Programming
    Replies: 2
    Last Post: 13th February 2008, 15:47
  3. Replies: 3
    Last Post: 18th October 2007, 18:07
  4. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

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.