Problem reading an Xml file with QXmlStreamReader
Page |
1 |
Ηi,
The code that follows reads a file and if a patient already exists, shows an error message, otherwise it calls the function
CreateXMLFile. The problem is that it gets in the while loop only once and the value of the id2 is always the same.
What can i do?
- void QXSRExample::ReadXMLFile(){
- flag=0;
- "QXSRExample::ReadXMLFile",
- "Couldn't open anna.xml",
- return;
- }
- while(!xml.atEnd() ) {
- if(xml.name() == "patient_id") {
- id2=xml.readElementText();
- if (itemId==id2)
- flag=1;}
- xml.readNext();
- }
- xml.clear();
- if (flag==1)
- else
- CreateXMLFile();
- file->close();
- }
- void QXSRExample::CreateXMLFile(){
- //Anoigma tou arxeiou
- //emfanisi sxetikou minimatos gia sfalma
- xmlWriter->setDevice(&file2);
- xmlWriter->writeStartElement("examinations");
- xmlWriter->writeStartElement("patient_exams");
- xmlWriter->writeStartElement("patient_id");
- xmlWriter->writeCharacters (itemId);
- xmlWriter->writeEndElement();
- ……………etc
28 replies
I mean a formatting like below, makes your code more readable and thus makes it easier to spot what is going wrong.
- void QXSRExample::ReadXMLFile(){
- flag=0;
- "QXSRExample::ReadXMLFile",
- "Couldn't open anna.xml",
- return;
- }
- while(!xml.atEnd() ) {
- if(xml.name() == "patient_id") {
- id2=xml.readElementText();
- if (itemId==id2)
- flag=1;
- }
- xml.readNext();
- }
- xml.clear();
- if (flag==1)
- else
- CreateXMLFile();
- file->close();
- }
I don’t think it is a good idea to try to write to a file from the function that is supposed to read from it. IMHO, it is better to separate that into different functions, and don’t call the one from the other.
In that way i had done the format of the code.
But that is not what you posted, so not what we could see and study.
ok, i will seperate the two functions but you didnt response to my problem.The problem is that it gets in the while loop only once and the value of the id2 is always the same.
it doesnt read the next patient_id
Could you post the XML that you are trying to read then? We can not check if the code is at fault in this case.
i write the xml but at the preview it is different.
- <examinations>
- <patient_exams>
- <patient_id>1</patient_id>
- <lab_test>
- <lab_id>Mikro</lab_id>
- <test_id>UREA</test_id>
- <specimen>blood</specimen>
- </lab_test>
- </patient_exams>
- </examinations>
the number 1 that is after examinations is after patient_id. and after examinations is the tag patient_exams
Edit: reformatted XML to make it more readable. Please do that yourself next time; Andre
With the above piece of XML, and the code that you showed, you will encounter the patient_id tag twice: once for the openening, and once for the closing tag. id2 will contain the text “1” on the first call, and be empty for the second (read the docs on readElementText for an explanation why).
It is obviously always the same, because the XML does not change all of a sudden during the reading (that is: you should not change the XML, as I told you before).
Have you considered using QDomDocument [doc.qt.nokia.com] instead?
This will create an in-memory representation of your XML document which allows for searching and inserting / removing nodes.
I change my code like this:
- void QXSRExample::ReadXMLFile(){
- flag=0;
- "QXSRExample::ReadXMLFile",
- "Couldn't open anna.xml",
- return;
- }
- while(!xml.atEnd() && !xml.hasError()){
- continue;
- if(xml.name() == "patient_id") {
- id2=xml.readElementText();
- if (itemId==id2)
- flag=1;
- }
- }
- xml.readNextStartElement();
- }
- }
- xml.clear();
- if (flag==1)
- else
- CreateXMLFile();
- file->close();
- }
but now the program craches.
Again: please make your code easy to read here. Take care of indentation and the likes.
Then, your code looks like nonsense. First you iterate through the whole XML in search of a StartDocument tag, and then you do nothing with it. Then, you expect there is more to read?
A crash is easy to debug usually. Your debugger will tell what line triggered the crash.
Yes, but QDomDocument is considdered EOL and is thus not wise to use in new code.
It is? It is not listed as deprecated in the documentation and the module maturiy list [labs.qt.nokia.com] also states it as done (not deprecated). Is there another source to look at when evaluating module lifetime?
You must log in to post a reply. Not a member yet? Register here!



