I created an xml file with QXmlStreamWriter, then I want to add each time a block before closing the xml file. but ,what I am trying to do is add the block to the end of the file with a startDocument, so in every addition I have a XML header, for this I got an error while reading the document, the program reads the first block and when it goes to the second block, it shows me "xml declaration not at strat of document" this is my file:
<?xml version="1.0" encoding="UTF-8"?>
<Fiche_Du_Patient>
<INFORMATIONS_DU_PATIENT>
<Hopital>a </Hopital>
<Num_dossier>b</Num_dossier>
<Nom_et_prenom>c</Nom_et_prenom>
<Date_de_naissance>11111</Date_de_naissance>
<Sex>F </Sex>
<Age>26</Age>
<Date_examen>22222</Date_examen>
<Medecin_traitant></Medecin_traitant>
<Rapport></Rapport>
</INFORMATIONS_DU_PATIENT>
<NUMERO_SLICE>
<Num_Slice>1</Num_Slice>
<COORDONEES>
<X1>7.32896</X1>
<Y1>10.6362</Y1>
<X2>8.96937</X2>
<Y2>9.28687</Y2>
</COORDONEES>
<DISTANCE>
<Distance_en_cm>2.1241</Distance_en_cm>
</DISTANCE>
<ANGLE>
<Angle>7</Angle>
</ANGLE>
</NUMERO_SLICE>
</Fiche_Du_Patient>
<?xml version="1.0" encoding="UTF-8"?>
<NUMERO_SLICE>
<Num_Slice>2</Num_Slice>
<COORDONEES>
<X1>7.80521</X1>
<Y1>10.3452</Y1>
<X2>9.49854</X2>
<Y2>9.525</Y2>
</COORDONEES>
<DISTANCE>
<Distance_en_cm>1.88152</Distance_en_cm>
</DISTANCE>
<ANGLE>
<Angle>1</Angle>
</ANGLE>
</NUMERO_SLICE>
<?xml version="1.0" encoding="UTF-8"?>
<NUMERO_SLICE>
<Num_Slice>3</Num_Slice>
<COORDONEES>
<X1>6.69396</X1>
<Y1>10.8215</Y1>
<X2>9.26042</X2>
<Y2>9.47208</Y2>
</COORDONEES>
<DISTANCE>
<Distance_en_cm>2.89957</Distance_en_cm>
</DISTANCE>
<ANGLE>
<Angle>25</Angle>
</ANGLE>
</NUMERO_SLICE>
this method is false, what I want to get is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Fiche_Du_Patient>
<INFORMATIONS_DU_PATIENT>
<Hopital>a </Hopital>
<Num_dossier>b</Num_dossier>
<Nom_et_prenom>c</Nom_et_prenom>
<Date_de_naissance>11111</Date_de_naissance>
<Sex>F </Sex>
<Age>26</Age>
<Date_examen>22222</Date_examen>
<Medecin_traitant></Medecin_traitant>
<Rapport></Rapport>
</INFORMATIONS_DU_PATIENT>
<NUMERO_SLICE>
<Num_Slice>1</Num_Slice>
<COORDONEES>
<X1>7.32896</X1>
<Y1>10.6362</Y1>
<X2>8.96937</X2>
<Y2>9.28687</Y2>
</COORDONEES>
<DISTANCE>
<Distance_en_cm>2.1241</Distance_en_cm>
</DISTANCE>
<ANGLE>
<Angle>7</Angle>
</ANGLE>
</NUMERO_SLICE>
<NUMERO_SLICE>
<Num_Slice>2</Num_Slice>
<COORDONEES>
<X1>7.80521</X1>
<Y1>10.3452</Y1>
<X2>9.49854</X2>
<Y2>9.525</Y2>
</COORDONEES>
<DISTANCE>
<Distance_en_cm>1.88152</Distance_en_cm>
</DISTANCE>
<ANGLE>
<Angle>1</Angle>
</ANGLE>
</NUMERO_SLICE>
<NUMERO_SLICE>
<Num_Slice>3</Num_Slice>
<COORDONEES>
<X1>6.69396</X1>
<Y1>10.8215</Y1>
<X2>9.26042</X2>
<Y2>9.47208</Y2>
</COORDONEES>
<DISTANCE>
<Distance_en_cm>2.89957</Distance_en_cm>
</DISTANCE>
<ANGLE>
<Angle>25</Angle>
</ANGLE>
</NUMERO_SLICE>
</Fiche_Du_Patient>
As you see < NUMERO_SLICE > block repeats with each addition of new parameters. Normally this block is added before the < / Fiche_Du_Patient> but I have not found a way with QXmlStreamWriter (I am a beginner in xml) I searched the net, I found nothing about this.
is it exist, please, a method that adds the < NUMERO_SLICE> block each time before ?
cordially
If you choose to continue with QXmlStreamReader/Writer, you'll need:
Below is an example if what you'd need to do. Keep in mind that there are a number of other cases to check for, like isComment(), copying attributes, etc:
The output (formatted):
I think that only this hack can avoid to read full XML to memory and rewrite:
this produce correct XML, but the format output isn't optimal (the nice indentation is lost). A better way would parse the last tag, without hard coding it.