Hello,
Is it possible to read an xml file line by line in Qt?
Hello,
Is it possible to read an xml file line by line in Qt?
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.
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?
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.
Dilshad (28th December 2010)
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.
you can use QTextStream::readLine () for reading a line at a time.but in QTextStream gives me the complete xml doc as a string.
==========================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.
I did do that but it reads the complete xml file as one string
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.
Hopefully. If not I will have to add my own breakpoints
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.
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.
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.
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.
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.
Thanks for the reply
Bookmarks