0

How can I write this to an xml file using qt?

 <model>
  <column loop="true">`enter code here`
    <item2  color="#4d6862"  />
    <item2  color="#ff0000ff" />
    <item2  color="#ff00ff00" />
    <item2 color="#ff00ffff" />
    <item2 color="#ffff0000" />
    <item2 color="#ffff00ff" />
    <item2  color="#ffffff00" />
    <item2 color="#4d6862" />
  </column>
</model>

This looks promising ( Writing XML Nodes in QtXML (QDomElement) ) but it doesn't mention attributes.

Community
  • 1
  • 1
Dave
  • 477
  • 2
  • 5
  • 18
  • `element.setAttribute(QLatin1String("loop"), QLatin1String("true"));` if the question is just about attributes. Otherwise, be more specific. – Frank Osterfeld Jan 28 '14 at 05:35

1 Answers1

0

You can do it with QXmlStreamWriter: http://qt-project.org/doc/qt-5/qxmlstreamwriter.html

QXmlStreamWriter writer (&file);    // a QFile object, must be open for writing
writer.setAutoFormatting(true);
writer.writeStartDocument("1.0");
writer.writeStartElement ("model");
writer.writeStartElement ("column");
    writer.writeAttribute ("loop", "true");

    writer.writeStartElement ("item2");
    writer.writeAttribute ("color", "#4d6862");
    writer.writeEndElement ();
    // write all other items

writer.writeEndDocument();   // this will close all open tags
Anton Poznyakovskiy
  • 2,109
  • 1
  • 20
  • 38
  • Look good. Thanks. I'm going to say it is answered as I won't be able to test for a couple days and unfortunately myopenID is closing down Feb 1st and I won't be able to log in again to do it properly. – Dave Jan 29 '14 at 05:43