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. Generating an XML file
Forum Updated to NodeBB v4.3 + New Features

Generating an XML file

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 14.2k 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.
  • A Offline
    A Offline
    Arukas
    wrote on last edited by
    #5

    Here is a sample XML

    @<?xml verision="1.0"?>
    <!-- Space for Rent -->
    <Forest>
    <Number_of_Trees>200</Trees>
    <Tree_1 bark="mossy" leaves="none" nest="lots"/>
    <Animals>
    <!-- Legally distinguable from Bambi -->
    <Rabbit>Thumperette</Rabbit>
    <Skunk>Rose</Skunk>
    </Animals>
    </Forest>@

    Here is how I started with the code:

    @QXmlStreamWriter writer(&file);
    writer.setAutoFormatting(true);
    writer.writeStartDocument("1.0");
    writer.writeComment("Space for Rent);@

    That is as far as I have gotten. I can't figure out how to make the <Forest> and <Animals> parts. I don't know what function covers that. I played around with the functions and still haven't figured out how to make it what I want to do.

    When I open it up in a text editior, everything is on the same line, which will ultimately make it hard to read for humans. And I haven't messed with the other stuff much because if I can't do the roots and sub roots, I'll have to use another method.

    -Arukas

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #6

      Well, you could continue like this:
      @
      writer.writeStartElement("Forest");
      writer.writeTextElement("Number_of_Trees", QString::number(200));
      writer.writeStartElement("Tree_1");
      writer.writeAttribute("bark", "mossy");
      writer.writeAttribute("leaves", "none");
      writer.writeAttribute("nest", "lots");
      writer.writeEndElement(); //Tree_1
      //...
      writer.writeEndElement(); //Forest;
      writer.writeEndDocument(); // End of document
      @

      Note: code not tested, just typed into the forum reply...

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Arukas
        wrote on last edited by
        #7

        I appreciate the help. That got me started on what I need to do, and works great. However, I still have one problem.

        This line
        @writer.setAutoFormatting(true);@

        The description says it suppose to add line breaks to add to the readability. However, it does not add any line breaks. All I can figure out is how to adjust the number of spaces.

        Thanks,
        Arukas

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #8

          That is strange... I have no clue what is going wrong for you then. It works for me.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Arukas
            wrote on last edited by
            #9

            I see the standard 4 spaces which is the default. Maybe I'm doing something wrong when I open the files, but I really doubt it.

            Is there a way I can force those line breaks?

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #10

              writer.setAutoFormatting(true) works for me.

              The lines are separated by a single newline, maybe your editor does not recognize that as a line break.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Arukas
                wrote on last edited by
                #11

                I've been using notepad to open files. I tried wordpad and it works fine.

                Now that I'm thinking, I had a problem like this before. My understanding, in the past you needed two things to get to the next line. You needed a carriage return to get back to the beginning of the line and a line feed to start a new line. Although, I'm not sure how to correct it. Only reason I think I know this is I was doing something in Linux and tried it in Windows and wondered why it didn't work.

                I think it, is how I am opening the file. This is the only way I know how to open files in Qt. Is a different way to correct the problem.

                @ QString filename="F.xml";
                QFile file;
                file.setFileName(filename);@

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on last edited by
                  #12

                  There are three kinds of marking a new line:

                  • LF (\n)
                    a single newline character
                    the usual way on Unix, Linux and Mac OS X
                  • CRLF (\r\n)
                    a carriage return followed by a newline
                    that's commonly used on Windows
                  • CR (\r)
                    a single carriage return
                    that was used by Mac OS up to version 9

                  See wikipedia on "Newline":http://en.wikipedia.org/wiki/Newline for some more details.

                  Notepad is known to work only with the traditional Windows CRLF line endings, while Wordpad is able to handle pure LF too. I personally avoid Notepad completely for that purpose.

                  Regarding your QFile questions:

                  You can combine the first two lines together into one:

                  @
                  QFile file("F.xml");
                  @

                  A possible solution to the line ending problem could be to open the file in text mode:

                  @
                  QFile file("F.xml");
                  if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
                  // handle the error here
                  }
                  @

                  bq. *The QIODevice::Text docs state:"
                  When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.

                  Make sure that the software/classes reading your XML can handle the CRLF too.

                  I personally avoid the QIODevice::Text flag, though. It's rarely needed nowadays.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #13

                    [quote author="Arukas" date="1323216964"]I've been using notepad to open files. I tried wordpad and it works fine.[/quote]
                    Might I suggest you get yourself a decent text editor, like "Notepad++":http://notepad-plus-plus.org/ perhaps?

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      fluca1978
                      wrote on last edited by
                      #14

                      Emacs runs even on Windows!

                      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