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. trying to use QXmlParser and friends
Forum Updated to NodeBB v4.3 + New Features

trying to use QXmlParser and friends

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 3 Posters 1.3k Views 3 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.
  • C Chris Kawa
    16 Apr 2020, 18:12

    This runs without error, but I'm not sure what it accomplishes.

    It parses the XML file i.e. walks through the entire structure splitting it to the little bits and pieces and reports them as callbacks to the handler. The default handler handles those pieces by doing nothing with them.

    So, do I have to subclass the default handler or not?

    Yes, specifically implement those virtual functions that handle the data you're interested in. For example if you're interested in an attribute of a particular node you could implement startElement() and look for the name you want.

    Alternatively you could subclass the base classes for given handler types, like QXmlErrorHandler or QXmlLexicalHandler , but those are pure virtual bases, meaning you would have to implement everything in them by yourself and chances are you're not interested in most of it. It's easier to subclass QXmlDefaultHandler which provides empty implementations for everything.

    M Offline
    M Offline
    mzimmers
    wrote on 16 Apr 2020, 18:35 last edited by
    #4

    @Chris-Kawa OK, progress is being made. I've sub-classed the default handler, and the startElement() and endElement() methods. In my other XML parser (expat) there was also a routine handleData() which was used to get the content between the start and end tags. I don't see an equivalent to that in QXmlDefaultHandler; how do I retrieve that data?

    Thanks...

    C 1 Reply Last reply 16 Apr 2020, 18:41
    0
    • M mzimmers
      16 Apr 2020, 18:35

      @Chris-Kawa OK, progress is being made. I've sub-classed the default handler, and the startElement() and endElement() methods. In my other XML parser (expat) there was also a routine handleData() which was used to get the content between the start and end tags. I don't see an equivalent to that in QXmlDefaultHandler; how do I retrieve that data?

      Thanks...

      C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 16 Apr 2020, 18:41 last edited by
      #5

      how do I retrieve that data?

      See QXmlDefaultHandler::characters().

      M 1 Reply Last reply 16 Apr 2020, 18:49
      1
      • C Chris Kawa
        16 Apr 2020, 18:41

        how do I retrieve that data?

        See QXmlDefaultHandler::characters().

        M Offline
        M Offline
        mzimmers
        wrote on 16 Apr 2020, 18:49 last edited by mzimmers
        #6

        @Chris-Kawa oh my goodness, that is a thing of beauty.

        Thanks to all who helped.

        EDIT: actually, I do have a follow-on question: this will allow me to build my map of tags and content. I need to export that map when I'm done. I'm thinking of making the map a member variable, and adding a method that returns it. Is there a better way to do this?

        C 1 Reply Last reply 16 Apr 2020, 19:43
        0
        • M mzimmers
          16 Apr 2020, 18:49

          @Chris-Kawa oh my goodness, that is a thing of beauty.

          Thanks to all who helped.

          EDIT: actually, I do have a follow-on question: this will allow me to build my map of tags and content. I need to export that map when I'm done. I'm thinking of making the map a member variable, and adding a method that returns it. Is there a better way to do this?

          C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 16 Apr 2020, 19:43 last edited by
          #7

          I'm thinking of making the map a member variable, and adding a method that returns it. Is there a better way to do this?

          So an XMLTagMapMakerHandler? Sounds good to me.

          1 Reply Last reply
          1
          • M Offline
            M Offline
            mzimmers
            wrote on 16 Apr 2020, 22:06 last edited by
            #8

            Well, I shouldn't have declared victory so quickly. When I migrated my test code into my app, I got this error:

            C:\wifibutton\utility\xmlparser.h:16: error: 'XmlHandler' does not name a type; did you mean 'XmlParser'?
                 XmlHandler m_handler;
                 ^~~~~~~~~~
                 XmlParser
            

            Here are some snippets:

            class XmlHandler : public QXmlDefaultHandler
            {
            private:
                MsgHash m_hash;
            public:
                XmlHandler();
            ...
            }
            
            #include "xmlhandler.h"
            
            class XmlParser
            {
                QXmlSimpleReader m_reader;
                XmlHandler m_handler;
               ...
            }
            

            Is there something special I need to know about sub-classing QXmlDefaultHandler, or am I just brain-fading here?

            Thanks...

            M 1 Reply Last reply 16 Apr 2020, 22:11
            0
            • M mzimmers
              16 Apr 2020, 22:06

              Well, I shouldn't have declared victory so quickly. When I migrated my test code into my app, I got this error:

              C:\wifibutton\utility\xmlparser.h:16: error: 'XmlHandler' does not name a type; did you mean 'XmlParser'?
                   XmlHandler m_handler;
                   ^~~~~~~~~~
                   XmlParser
              

              Here are some snippets:

              class XmlHandler : public QXmlDefaultHandler
              {
              private:
                  MsgHash m_hash;
              public:
                  XmlHandler();
              ...
              }
              
              #include "xmlhandler.h"
              
              class XmlParser
              {
                  QXmlSimpleReader m_reader;
                  XmlHandler m_handler;
                 ...
              }
              

              Is there something special I need to know about sub-classing QXmlDefaultHandler, or am I just brain-fading here?

              Thanks...

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 16 Apr 2020, 22:11 last edited by
              #9

              @mzimmers
              Just to be sure its not that.
              Does xmlparser.h include XmlHandler.h and reverse ?
              (circular include)

              M 1 Reply Last reply 16 Apr 2020, 22:15
              0
              • M mrjj
                16 Apr 2020, 22:11

                @mzimmers
                Just to be sure its not that.
                Does xmlparser.h include XmlHandler.h and reverse ?
                (circular include)

                M Offline
                M Offline
                mzimmers
                wrote on 16 Apr 2020, 22:15 last edited by
                #10

                @mrjj no. xmlparser.h includes xmlhandler.h, but not the other way around.

                M 1 Reply Last reply 16 Apr 2020, 22:17
                0
                • M mzimmers
                  16 Apr 2020, 22:15

                  @mrjj no. xmlparser.h includes xmlhandler.h, but not the other way around.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 16 Apr 2020, 22:17 last edited by
                  #11

                  @mzimmers
                  Ok, just had to be sure :)

                  It simply says it don't know type so it don't see the include for some reason.

                  Did you try clean all, rebuild ?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mzimmers
                    wrote on 16 Apr 2020, 22:21 last edited by
                    #12

                    Yes, I did.

                    Here's the complete code from the headers:

                    xmlhandler.h

                    #ifndef XMLHANDLER_H
                    #define XMLHANDLER_H
                    
                    
                    #include <QXmlDefaultHandler>
                    
                    #include "message.h"
                    
                    using namespace std;
                    
                    class XmlHandler : public QXmlDefaultHandler
                    {
                    private:
                        MsgHash m_hash;
                        bool m_tagStarted = false;
                        QString m_tag;
                        QString m_data;
                        QXmlInputSource *m_source;
                    
                    public:
                        XmlHandler();
                        ~XmlHandler();
                        bool startElement(const QString &uri,
                                          const QString &local,
                                          const QString &name,
                                          const QXmlAttributes &atts) override;
                        bool endElement(const QString &uri,
                                          const QString &local,
                                          const QString &name) override;
                        bool characters(const QString &ch) override;
                        void getHash(MsgHash &msgHash) {msgHash = m_hash;}
                    };
                    
                    
                    #endif // XMLHANDLER_H
                    

                    xmlparser.h:

                    #ifndef XMLPARSER_H
                    #define XMLPARSER_H
                    
                    #include <stdint.h>
                    
                    #include <QXmlDefaultHandler>
                    #include <QXmlSimpleReader>
                    
                    #include "constants.h"
                    #include "message.h"
                    #include "xmlhandler.h"
                    
                    class XmlParser
                    {
                        QXmlSimpleReader m_reader;
                        XmlHandler m_handler;
                    public:
                        XmlParser();
                        ~XmlParser();
                        int process(string xml, MsgHash &msgHash);
                    };
                    
                    #endif // XMLPARSER_H
                    
                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 16 Apr 2020, 22:24 last edited by
                      #13

                      Hi
                      It looks ok.
                      Could you try add some dummy class to the xmlhandler.h

                      class Test {
                      };

                      and see if it can see that symbol ?

                      just to test it dont see the include and not something with class XmlHandler

                      M 1 Reply Last reply 16 Apr 2020, 22:27
                      0
                      • M mrjj
                        16 Apr 2020, 22:24

                        Hi
                        It looks ok.
                        Could you try add some dummy class to the xmlhandler.h

                        class Test {
                        };

                        and see if it can see that symbol ?

                        just to test it dont see the include and not something with class XmlHandler

                        M Offline
                        M Offline
                        mzimmers
                        wrote on 16 Apr 2020, 22:27 last edited by mzimmers
                        #14

                        @mrjj good test -- it didn't see that either. I wonder if XMLHANDLER_H is a reserved term...?

                        ANSWER: no it isn't...

                        M 1 Reply Last reply 16 Apr 2020, 22:36
                        0
                        • M mzimmers
                          16 Apr 2020, 22:27

                          @mrjj good test -- it didn't see that either. I wonder if XMLHANDLER_H is a reserved term...?

                          ANSWER: no it isn't...

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 16 Apr 2020, 22:36 last edited by
                          #15

                          @mzimmers

                          Hmm can you check your .pro file its not listed 2 times ?

                          Also
                          can you please inspect both files
                          with Include Hierarchy?

                          alt text
                          The last.

                          and see if you see anything odd.

                          M 2 Replies Last reply 16 Apr 2020, 22:45
                          1
                          • M mrjj
                            16 Apr 2020, 22:36

                            @mzimmers

                            Hmm can you check your .pro file its not listed 2 times ?

                            Also
                            can you please inspect both files
                            with Include Hierarchy?

                            alt text
                            The last.

                            and see if you see anything odd.

                            M Offline
                            M Offline
                            mzimmers
                            wrote on 16 Apr 2020, 22:45 last edited by
                            #16

                            @mrjj that wasn't it exactly, but you put me on the right track. I'll report back in a bit.

                            Thanks...

                            1 Reply Last reply
                            0
                            • M mrjj
                              16 Apr 2020, 22:36

                              @mzimmers

                              Hmm can you check your .pro file its not listed 2 times ?

                              Also
                              can you please inspect both files
                              with Include Hierarchy?

                              alt text
                              The last.

                              and see if you see anything odd.

                              M Offline
                              M Offline
                              mzimmers
                              wrote on 16 Apr 2020, 22:59 last edited by
                              #17

                              @mrjj great suggestion there! Long story short, I had a cyclic dependency. Thanks for your help on this.

                              1 Reply Last reply
                              1
                              • M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 16 Apr 2020, 23:03 last edited by
                                #18

                                Super :)
                                Well it's my number one suspect when it says "don't know symbol"
                                and you check you have the header included and all seem fine.

                                1 Reply Last reply
                                0

                                13/18

                                16 Apr 2020, 22:24

                                • Login

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