Results 1 to 18 of 18

Thread: read xml line by line

  1. #1
    Join Date
    Jul 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default read xml line by line

    Hello,
    Is it possible to read an xml file line by line in Qt?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: read xml line by line

    Yes, it is possible.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read xml line by line

    Thanks for the reply.
    Could you please tell me how this is done.
    I need to read onle line e.g first tag <abc> from one xml file and then compare <abc> to a tag in another file.
    How do I convert the line read into a string?

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: read xml line by line

    I could.
    But you SHOULD start by reading the documentation, its far more extensive then I can explain to you here.
    The forum is more for when you have troule understanding things in the docs, or if you run in trouble with your implementation, the "how to" is in the documentation, and you should start there.
    Here are some pointers:
    XmlModule
    QFile
    QTextStream
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    Dilshad (28th December 2010)

  6. #5
    Join Date
    Jul 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read xml line by line

    Thanks.
    I already tried implementaing it using QFile and QTextStream, but in QTextStream gives me the complete xml doc as a string.
    I will go through the XMLModule.

  7. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: read xml line by line

    but in QTextStream gives me the complete xml doc as a string.
    you can use QTextStream::readLine () for reading a line at a time.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #7
    Join Date
    Jul 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read xml line by line

    I did do that but it reads the complete xml file as one string

  9. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: read xml line by line

    Ok, that means that the xml file has no break lines in it.
    Then I think the XML classes will be more helpful.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  10. #9
    Join Date
    Jul 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read xml line by line

    Hopefully. If not I will have to add my own breakpoints

  11. #10
    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: read xml line by line

    Quote Originally Posted by Dilshad View Post
    I need to read onle line e.g first tag <abc> from one xml file and then compare <abc> to a tag in another file.
    There is a difference between reading line by line and reading tag by tag. Tags don't have to be in separate lines - one tag can occupy multiple lines and one line can contain multiple tags. Check your file first with a text editor to see how it is organized.
    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.


  12. #11
    Join Date
    Jul 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read xml line by line

    The tags appear in one line.
    There are tagas such as <range = "10">
    and also like <from>10</from>. I need to compare line by line.

  13. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: read xml line by line

    Just read both files with the XML module, and iterate through the tags.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #13
    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: read xml line by line

    Quote Originally Posted by Dilshad View Post
    The tags appear in one line.
    There are tagas such as <range = "10">
    and also like <from>10</from>. I need to compare line by line.
    Are you interested in syntactical or semantical comparison?
    <range = "10"> is not a valid xml entry so no XML module in Qt will read it.
    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.


  15. #14
    Join Date
    Jul 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read xml line by line

    Thanks,
    I checked with 2 different files. It is a problem with missing line breaks.
    A file created with a specific tool does not add line breaks, and I was testing my code with that xml file, whereas I found an already available xml file and it works fine.

  16. #15
    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: read xml line by line

    Quote Originally Posted by Dilshad View Post
    I checked with 2 different files. It is a problem with missing line breaks.
    Line breaks might not be missing. Files might have been created in unix environment where line breaks are encoded differently than on Windows. So you might need to transcode line endings first before reading the files under Windows. Some applications (like WordPad) interprets unix line breaks correctly - see if it can see them in your files.
    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.


  17. #16
    Join Date
    Jul 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read xml line by line

    It displays the file in one line in WordPad

  18. #17
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: read xml line by line

    Quote Originally Posted by Dilshad View Post
    It displays the file in one line in WordPad
    Then you don't want to read the file line by line; you want to read it tag by tag. Which is the correct way to approach this problem anyway; XML doesn't specify any formatting, so to work properly you can't count on a file being formatted in any particular way.

    Read a small chunk of the file (or a large chunk, or even all of it) into memory. Then scan for the opening and closing brackets, take the text between them as the content of the first tag, and proceed. Or simply use XML parsing to simply walk through the tags one by one. But forget the whole "line by line" idea; it's wrong.

  19. #18
    Join Date
    Jul 2010
    Posts
    25
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read xml line by line

    Thanks for the reply

Similar Threads

  1. How to read and write line by line?
    By Alex Snet in forum Qt Programming
    Replies: 3
    Last Post: 28th November 2010, 15:49
  2. Replies: 3
    Last Post: 13th August 2010, 11:50
  3. read QTextBrowser line by line
    By navid in forum Newbie
    Replies: 1
    Last Post: 1st March 2010, 15:05
  4. HOw to read Line edit through a for loop
    By raghvendramisra in forum Qt Programming
    Replies: 11
    Last Post: 30th August 2007, 03:36
  5. How to read line from file
    By Krishnacins in forum Newbie
    Replies: 10
    Last Post: 1st June 2006, 23:14

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.