Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. issue with QXmlStreamWriter
Qt 6.12 LTS is out! See what's new in the release blog

issue with QXmlStreamWriter

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.2k Views 2 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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;
      }
      
      1 Reply Last reply
      2
      • hskoglundH Online
        hskoglundH Online
        hskoglund
        wrote on last edited by
        #3

        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

        mzimmersM 1 Reply Last reply
        2
        • mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • hskoglundH hskoglund

            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

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

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

            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
            • Unsolved