PDA

View Full Version : read xml line by line



Dilshad
28th December 2010, 09:04
Hello,
Is it possible to read an xml file line by line in Qt?

high_flyer
28th December 2010, 09:07
Yes, it is possible.

Dilshad
28th December 2010, 09:17
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?

high_flyer
28th December 2010, 09:21
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 (http://doc.trolltech.com/4.7/qtxml.html)
QFile

QTextStream

Dilshad
28th December 2010, 09:26
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.

high_flyer
28th December 2010, 09:37
but in QTextStream gives me the complete xml doc as a string.
you can use QTextStream::readLine () for reading a line at a time.

Dilshad
28th December 2010, 09:38
I did do that but it reads the complete xml file as one string

high_flyer
28th December 2010, 09:40
Ok, that means that the xml file has no break lines in it.
Then I think the XML classes will be more helpful.

Dilshad
28th December 2010, 09:47
Hopefully. If not I will have to add my own breakpoints :)

wysota
28th December 2010, 09:53
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.

Dilshad
28th December 2010, 10:24
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.

high_flyer
28th December 2010, 10:26
Just read both files with the XML module, and iterate through the tags.

wysota
28th December 2010, 10:36
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.

Dilshad
28th December 2010, 10:37
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.

wysota
28th December 2010, 10:48
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.

Dilshad
28th December 2010, 10:50
It displays the file in one line in WordPad

SixDegrees
28th December 2010, 11:17
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.

Dilshad
28th December 2010, 11:34
Thanks for the reply