PDA

View Full Version : Format XML file



vikuseth
19th December 2012, 07:33
I have a xml file where outputs are not getting formatted . That means all the outputs are in a single line but i want to break it tag by tag .

For e.g. -


<?xml version="1.0" encoding="UTF-8" standalone="no" ?><Analyser> <JointDetails> <Details><StdThickness> T </StdThickness><Thickness_num> 0.032 </Thickness_num></Details> </JointDetails></Analyser>

But i want to do it like this ::


<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Analyser>
<JointDetails>
<Details>
<StdThickness> T </StdThickness>
<Thickness_num> 0.032 </Thickness_num>
</Details>
</JointDetails>
</Analyser>

Please dont suggest to do it while writting the XML file because this xml file is already there but now i have to format it as mentioned above .

Thanks in advance .

sonulohani
19th December 2012, 08:26
#include <QCoreApplication>
#include <QDebug>
#include <QXmlStreamWriter>
#include <QFile>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile file("demo.xml");
file.open(QIODevice::WriteOnly);
QXmlStreamWriter stream(&file);
stream.setAutoFormatting(true);
stream.writeStartDocument();
stream.writeStartElement("Analyser");
stream.writeStartElement("JointDetails");
stream.writeStartElement("Details");
stream.writeTextElement("StdThickness","T");
stream.writeTextElement("Thickness_num","0.032");
stream.writeEndElement();
stream.writeEndElement();
stream.writeEndElement();
stream.writeEndDocument();
file.close();
return a.exec();
}

vikuseth
20th December 2012, 06:59
I don't want to write a XML file . The XML file is already there , just i need to format it since its in a single line . I want to break it into tag by tag .

Santosh Reddy
20th December 2012, 08:48
void format(void)
{
QDomDocument input;

QFile inFile("D:/input.xml");
QFile outFile("D:/output.xml");

inFile.open(inFile.Text | inFile.ReadOnly);
outFile.open(outFile.Text | outFile.WriteOnly);

input.setContent(&inFile);

QDomDocument output(input);
QTextStream stream(&outFile);
output.save(stream, 2);
}

ChrisW67
20th December 2012, 22:31
I don't want to write a XML file . The XML file is already there , just i need to format it since its in a single line . I want to break it into tag by tag .

Sure you do. You cannot transform xml A into xml B without writing xml B somwhere. If you don't want to save the result in a permanent file then save it to a QBuffer or QByteArray using QXmlStreamReader/QXmlStreamWriter or DomDocument. The DomDocument option requires the QtXml module but is less effort.

vikuseth
21st December 2012, 07:57
void format(void)
{
QDomDocument input;

QFile inFile("D:/input.xml");
QFile outFile("D:/output.xml");

inFile.open(inFile.Text | inFile.ReadOnly);
outFile.open(outFile.Text | outFile.WriteOnly);

input.setContent(&inFile);

QDomDocument output(input);
QTextStream stream(&outFile);
output.save(stream, 2);
}


What i have to do if i have to save the contents inside the same file . like in the above case for D:/input.xml .

ChrisW67
21st December 2012, 09:16
Write the output to a temporary file and, when that is successfully closed rename it to replace the original.
Or, write the result to a buffer in memory and when that is complete write the buffer to the file.