Results 1 to 7 of 7

Thread: Format XML file

  1. #1
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Format XML file

    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. -

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8" standalone="no" ?><Analyser> <JointDetails> <Details><StdThickness> T </StdThickness><Thickness_num> 0.032 </Thickness_num></Details> </JointDetails></Analyser>
    To copy to clipboard, switch view to plain text mode 

    But i want to do it like this ::

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    2. <Analyser>
    3. <JointDetails>
    4. <Details>
    5. <StdThickness> T </StdThickness>
    6. <Thickness_num> 0.032 </Thickness_num>
    7. </Details>
    8. </JointDetails>
    9. </Analyser>
    To copy to clipboard, switch view to plain text mode 

    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 .
    Last edited by vikuseth; 19th December 2012 at 06:37.

  2. #2
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Format XML file

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDebug>
    3. #include <QXmlStreamWriter>
    4. #include <QFile>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9. QFile file("demo.xml");
    10. file.open(QIODevice::WriteOnly);
    11. QXmlStreamWriter stream(&file);
    12. stream.setAutoFormatting(true);
    13. stream.writeStartDocument();
    14. stream.writeStartElement("Analyser");
    15. stream.writeStartElement("JointDetails");
    16. stream.writeStartElement("Details");
    17. stream.writeTextElement("StdThickness","T");
    18. stream.writeTextElement("Thickness_num","0.032");
    19. stream.writeEndElement();
    20. stream.writeEndElement();
    21. stream.writeEndElement();
    22. stream.writeEndDocument();
    23. file.close();
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 
    Heavy Metal Rules. For those about to rock, we salute you.
    HIT THANKS IF I HELPED.

  3. #3
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Format XML file

    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 .

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Format XML file

    Qt Code:
    1. void format(void)
    2. {
    3. QDomDocument input;
    4.  
    5. QFile inFile("D:/input.xml");
    6. QFile outFile("D:/output.xml");
    7.  
    8. inFile.open(inFile.Text | inFile.ReadOnly);
    9. outFile.open(outFile.Text | outFile.WriteOnly);
    10.  
    11. input.setContent(&inFile);
    12.  
    13. QDomDocument output(input);
    14. QTextStream stream(&outFile);
    15. output.save(stream, 2);
    16. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. The following user says thank you to Santosh Reddy for this useful post:

    vikuseth (21st December 2012)

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Format XML file

    Quote Originally Posted by vikuseth View Post
    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.

  7. #6
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Format XML file

    Quote Originally Posted by Santosh Reddy View Post
    Qt Code:
    1. void format(void)
    2. {
    3. QDomDocument input;
    4.  
    5. QFile inFile("D:/input.xml");
    6. QFile outFile("D:/output.xml");
    7.  
    8. inFile.open(inFile.Text | inFile.ReadOnly);
    9. outFile.open(outFile.Text | outFile.WriteOnly);
    10.  
    11. input.setContent(&inFile);
    12.  
    13. QDomDocument output(input);
    14. QTextStream stream(&outFile);
    15. output.save(stream, 2);
    16. }
    To copy to clipboard, switch view to plain text mode 
    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 .

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Format XML file

    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.

  9. The following user says thank you to ChrisW67 for this useful post:

    vikuseth (21st December 2012)

Similar Threads

  1. Replies: 5
    Last Post: 1st June 2011, 08:28
  2. Replies: 0
    Last Post: 21st July 2010, 10:32
  3. read the file which has the format of odp!
    By sunnysun520 in forum Qt Programming
    Replies: 11
    Last Post: 14th May 2009, 15:14
  4. QImage, get file format
    By greenvirag in forum Qt Programming
    Replies: 6
    Last Post: 29th December 2008, 13:13
  5. QSettings, my own format of config-file
    By IPFreely in forum Qt Programming
    Replies: 2
    Last Post: 14th November 2007, 20:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.