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.0k 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 6 Dec 2011, 16:57 last edited by
    #1

    I am trying to generate an XML file using the xml streaing classes in Qt. I running into a problem. Qt's XML writer's documentation is too technical for me. I can write a XML file by brute force but I rather not.

    So my question, anyone know where I can get a pragmatic tutorial on XML? I did some searching around and have not found anything useable yet.

    Thanks,
    Arukas

    [fixed typo in title, Eddy]

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Arukas
      wrote on 6 Dec 2011, 18:02 last edited by
      #2

      Let me add some more to that. I have read some more tutorials and I'm finding that I am thinking I am having more problems with the XML Streaming than the actual XML.

      I found some XML streaming examples, and they don't do what I am looking for. It's putting everything on the same line. I'd like the tags and comments to be on their own line. I like to add roots, but I can't figure that out.

      About all I can do is start an XML document and add an element. I can't really figure anything else out. The XML file I am working on is pretty simple, but I can't figure out how to make XML streaming work for me.

      -Arukas

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Arukas
        wrote on 6 Dec 2011, 18:22 last edited by
        #3

        I found one of the problems to my questions, but it made another problem. There's a setAutoFormatting to true, but it does no autoformating. I also followed the example in the Qt Help file.

        I'm still having problem creating roots.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 6 Dec 2011, 19:20 last edited by
          #4

          Could you show a small sample of the XML you'd like to generate, and the code you are now using to try to generate that?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Arukas
            wrote on 6 Dec 2011, 20:04 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 6 Dec 2011, 20:29 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 6 Dec 2011, 20:52 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 6 Dec 2011, 20:56 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 6 Dec 2011, 23:14 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 6 Dec 2011, 23:20 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 7 Dec 2011, 00:16 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 7 Dec 2011, 00:48 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 7 Dec 2011, 07:49 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 7 Dec 2011, 08:39 last edited by
                              #14

                              Emacs runs even on Windows!

                              1 Reply Last reply
                              0

                              1/14

                              6 Dec 2011, 16:57

                              • Login

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