Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Writing formatted cdata with qXmlStreamWriter
I'm trying to write a geometry exporter which needs to match the input file for a specific program. The part I'm having trouble with is writing out cdata to match the following. Is this possible to do with qXmlStreamWriter or should I use something like xmlWriter? I'd like to write directly to the file since the data set could be pretty large. I've tried using writeCDATA but can't figure out how to do the multiline formatting.
<TABLE TYPE="COORDINATE.CARTESIAN" > <![CDATA[ | ID X Y Z | ; 1 -0.111825 -0.148800 0.099180 2 -0.124244 0.152925 0.102128 3 -0.130284 -0.088804 0.482442 4 0.174188 0.151138 0.004559 ]]> </TABLE>
Thanks for any help pointing me in the right direction.
Be aware that the need to use
CDATA/your desired layout is totally for your own, human readability. Assuming an XML reader is going to parse this, you never need
That said, the source simply copies what you supply into the
CDATAsection, so presumably
\nshould generate your desired newlines? (You'll have to handle the indentation yourself if you care about that...)
This post is deleted!
Rodbrew last edited by Rodbrew
This is what I came up with. If there is a more elegant way to do this it would be great to know. But this isn't too bad I don't think.
``` QXmlStreamWriter outXml; QString filename = QFileDialog::getSaveFileName(this, "Save Xml", ".", "Xml files (*.xml)"); QFile outFile(filename); if (!outFile.open(QIODevice::ReadWrite | QIODevice::Text)) qDebug() << "Error saving XML file."; outXml.setDevice(&outFile); outXml.autoFormatting(); outXml.setAutoFormatting(true); // // Other Code // outXml.writeStartElement("TABLE"); outXml.writeAttribute("TYPE","COORDINATE.CARTESIAN"); outXml.writeCharacters("\n"); QString CDataStart( "\t\t<![CDATA[\n"); QByteArray qbaCDataStart = CDataStart.toUtf8(); outXml.device()->write(qbaCDataStart); outXml.writeCharacters("\t\t\t| ID X Y Z |\n"); outXml.writeCharacters("\t\t\t;\n"); outXml.writeCharacters("\t\t\t999999 0.0 0.0 0.0\n"); outXml.writeCharacters("\t\t\t1 -0.111825 -0.148800 0.099180\n"); outXml.writeCharacters("\t\t\t2 -0.124244 0.152925 0.102128\n"); QString CDataEnd( "\t\t]]>\n"); QByteArray qbaCDataEnd = CDataEnd.toUtf8(); outXml.device()->write(qbaCDataEnd);
Here's the output ``` <TABLE TYPE="COORDINATE.CARTESIAN"> <![CDATA[ | ID X Y Z | ; 999999 0.0 0.0 0.0 1 -0.111825 -0.148800 0.099180 2 -0.124244 0.152925 0.102128 ]]> </TABLE>
Anyway hope this helps others. Doesn't seem to be very many good examples out there.
Yep, that's fine for exactly what you want, there's nothing better, though I didn't realize you'd have exactly those numbers typed into literal code :)
Yeah that was just a test. I'd actually loop through the model data and write it out on the fly. Could be thousands of vertex points that's why I wanted to stream it. Just couldn't find any good examples other then qDomDocument method which seems like is somewhat outdated and writes the file to memory. Kind of new to xml and qt so I was kind of banging my head for awhile.
I have small XML documents and find
QDomDocumentconvenient. Yes, you want to stick with streaming for large data, DOM document would indeed use memory.