Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Writing formatted cdata with qXmlStreamWriter
Forum Update on Monday, May 27th 2025

Writing formatted cdata with qXmlStreamWriter

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rodbrew
    wrote on 12 Mar 2019, 23:32 last edited by
    #1

    Hi,

    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.

    J 1 Reply Last reply 13 Mar 2019, 09:56
    0
    • R Rodbrew
      12 Mar 2019, 23:32

      Hi,

      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.

      J Offline
      J Offline
      JonB
      wrote on 13 Mar 2019, 09:56 last edited by
      #2

      @Rodbrew
      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 CDATA.

      That said, the source simply copies what you supply into the CDATA section, so presumably \n should generate your desired newlines? (You'll have to handle the indentation yourself if you care about that...)

      1 Reply Last reply
      1
      • R Offline
        R Offline
        Rodbrew
        wrote on 13 Mar 2019, 16:59 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rodbrew
          wrote on 13 Mar 2019, 19:05 last edited by Rodbrew
          #4

          Thanks JonB,

          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.

          J 1 Reply Last reply 13 Mar 2019, 19:22
          2
          • R Rodbrew
            13 Mar 2019, 19:05

            Thanks JonB,

            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.

            J Offline
            J Offline
            JonB
            wrote on 13 Mar 2019, 19:22 last edited by
            #5

            @Rodbrew
            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 :)

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rodbrew
              wrote on 13 Mar 2019, 19:39 last edited by
              #6

              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.

              J 1 Reply Last reply 13 Mar 2019, 20:44
              1
              • R Rodbrew
                13 Mar 2019, 19:39

                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.

                J Offline
                J Offline
                JonB
                wrote on 13 Mar 2019, 20:44 last edited by
                #7

                @Rodbrew
                I have small XML documents and find QDomDocument convenient. Yes, you want to stick with streaming for large data, DOM document would indeed use memory.

                1 Reply Last reply
                0

                1/7

                12 Mar 2019, 23:32

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved