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 Updated to NodeBB v4.3 + New Features

Writing formatted cdata with qXmlStreamWriter

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.1k Views 1 Watching
  • 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 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.

    JonBJ 1 Reply Last reply
    0
    • R Rodbrew

      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.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on 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 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rodbrew
          wrote on 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.

          JonBJ 1 Reply Last reply
          2
          • R Rodbrew

            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.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on 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 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.

              JonBJ 1 Reply Last reply
              1
              • R Rodbrew

                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.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on 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

                • Login

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