XML To FastInfoset Converter Example
Example below demonstrates how XML file can be converted to FastInfoset file and vice versa.
#include <FastInfosetStreamReader> #include <FastInfosetStreamWriter> #include <QXmlStreamReader> #include <QXmlStreamWriter> template<typename StreamReader, typename StreamWriter> void convert_xml(StreamReader* reader, StreamWriter* writer) { while (reader->readNext() != QXmlStreamReader::Invalid) { switch (reader->tokenType()) { case QXmlStreamReader::StartDocument: { QString version = reader->documentVersion().toString(); if (version.isEmpty()) version = "1.0"; if (reader->isStandaloneDocument()) writer->writeStartDocument(version, reader->isStandaloneDocument()); else writer->writeStartDocument(version); } break; case QXmlStreamReader::EndDocument: { writer->writeEndDocument(); } break; case QXmlStreamReader::StartElement: { QXmlStreamNamespaceDeclarations namespaceDeclarations = reader->namespaceDeclarations(); for (int i = 0; i < namespaceDeclarations.size(); ++i) { const QXmlStreamNamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.at(i); writer->writeNamespace(namespaceDeclaration.namespaceUri().toString(), namespaceDeclaration.prefix().toString()); } writer->writeStartElement(reader->namespaceUri().toString(), reader->name().toString()); writer->writeAttributes(reader->attributes()); } break; case QXmlStreamReader::EndElement: { writer->writeEndElement(); } break; case QXmlStreamReader::Characters: { if (reader->isCDATA()) writer->writeCDATA(reader->text().toString()); else writer->writeCharacters(reader->text().toString()); } break; case QXmlStreamReader::Comment: { writer->writeComment(reader->text().toString()); } break; case QXmlStreamReader::ProcessingInstruction: { writer->writeProcessingInstruction(reader->processingInstructionTarget().toString(), reader->processingInstructionData().toString()); } break; default: break; } if (reader->atEnd()) break; } if (reader->hasError()) { QMessageBox::critical(qApp->activeWindow(), "Stream read error", reader->errorString()); return; } } int main(int argc, char *argv[]) { QFile xmlFile, fastInfosetFile; //Convert XML to FastInfoset QXmlStreamReader xmlreader; xmlreader.setDevice(&fastInfosetFile); FastInfosetStreamWriter xmlwriter; xmlwriter.setDevice(&xmlFile); convert_xml<QXmlStreamReader, FastInfosetStreamWriter>(&xmlreader, &xmlwriter); ... //Convert FastInfoset to XML FastInfosetStreamReader xmlreader; xmlreader.setDevice(&fastInfosetFile); QXmlStreamWriter xmlwriter; xmlwriter.setDevice(&xmlFile); convert_xml<FastInfosetStreamReader, QXmlStreamWriter>(&xmlreader, &xmlwriter); ... }
[Missing image fastinfoset_examples_XMLConverter.png]
Files:
- fastinfoset/XMLConverter/XMLConverter.pro
- fastinfoset/XMLConverter/XMLConverter.py
- fastinfoset/XMLConverter/main.cpp
- fastinfoset/XMLConverter/mainwindow.cpp
- fastinfoset/XMLConverter/mainwindow.h
- fastinfoset/XMLConverter/mainwindow.ui
- fastinfoset/XMLConverter/test1.xml
- fastinfoset/XMLConverter/test2.xml
- fastinfoset/XMLConverter/xmlsyntaxhighlighter.cpp
- fastinfoset/XMLConverter/xmlsyntaxhighlighter.h
- fastinfoset/XMLConverter/xmlsyntaxhighlighter.py