issue with QXmlStreamWriter



  • I'm using QXmlStreamWriter to write to a QString object.

    The device I'm communicating with doesn't fully adhere to XML standards; the starting tag must be simply "<XML>". This wouldn't seem like a big deal, except that I can't use writeCharacters() because it escapes the angle brackets. I can use writeStartElement("XML") but this gives me "<XML" (unclosed).

    I can use writeStartElement("XML>") and this works, but strikes me as somewhat hokey.

    As an alternative, I suppose I could fully form the string, then edit out the stuff I can't have in the header.

    Any other opinions on this? Getting the format of the XML changed isn't an option.


  • Moderators

    Hi! Little hackish, too, but seems to work:

    #include <QApplication>
    #include <QDebug>
    #include <QXmlStreamWriter>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QString output;
        output += "<XML>";
    
        QXmlStreamWriter stream(&output);
        stream.setAutoFormatting(true);
        stream.writeStartElement("bookmark");
        stream.writeAttribute("href", "http://qt-project.org/");
        stream.writeTextElement("title", "Qt Project");
        stream.writeEndElement(); // bookmark
        stream.writeEndDocument();
    
        qDebug() << output;
    
        return 0;
    }
    


  • Hi, I had more or less the same problem (receiving XML from Microsoft's IIS6 with way too much stuff), solved it by using XPath to strip away the junk. Perhaps you can use XPath as well to reformat your XML file, it's here



  • Yeah, that's probably a better approach than mine, as it keeps the "<>" characters out of the stream handling. Thanks.



  • @hskoglund that's probably a more correct way of doing it, but for simplicity's sake, I'm going to use Wieland's approach.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.