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. XML parsing
Qt 6.11 is out! See what's new in the release blog

XML parsing

Scheduled Pinned Locked Moved Unsolved General and Desktop
xml
8 Posts 4 Posters 2.5k 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.
  • A Offline
    A Offline
    akshay123
    wrote on last edited by A Former User
    #1

    I am new to Qt

    I am developing application to parse the XML file. But the File format looks like this

    <Class name="ABC">
        <LayoutEntry name="ABB"            type="class:ABB"/>
        <LayoutEntry name="ACC"            type="class:ACC"/>
        <LayoutEntry name="ADD"            type="class:ADD"/>
     </Class>
    
     <Class name="ABB">   //Declared  in class ABC
        <LayoutEntry name="ABB1"            type="class:ABB1"/>
        <LayoutEntry name="ABB2"            type="class:ABB2"/>
      </Class>
    
      <Class name="ABB1">  //Declared in class ABB
        <LayoutEntry name="ABB1"            value=ABB1"/>
      </Class>
    
      <Class name="ABB2">
        <LayoutEntry name="ABB2"            value=ABB2"/>
      </Class>
    

    Please some one suggest me for any library or method in Qt to parse this type of XML . Thanks

    Edit: Added code tags -- @Wieland

    jsulmJ 1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by JohanSolo
      #2

      You should give a look at the docs. If you only want to read files, QXmlStreamReader does everything you need.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      5
      • A Offline
        A Offline
        akshay123
        wrote on last edited by
        #3

        since you can see that there is a class inside class i.e nested class , Which library will be fine for doing this

        1 Reply Last reply
        0
        • yuvaramY Offline
          yuvaramY Offline
          yuvaram
          wrote on last edited by
          #4

          Hi @akshay123 ,

          Slide modification in have made for the XML provided, i have added root tag <ClassRoot>.
          Below program may help you.
          void xmlparsing::DecodeXML()
          {
          qDebug()<<Q_FUNC_INFO<<endl;
          QDomDocument DdocObj;
          QFile fobj("class.xml");
          if (!fobj.open(QIODevice::ReadOnly ))
          {
          qWarning() << "Error ::"<<fobj.errorString() << endl;
          }
          DdocObj.setContent(&fobj);
          fobj.close();
          QDomElement root =DdocObj.documentElement();

          if(root.tagName().compare("ClassRoot")==0){
              QDomNodeList CNode1 = root.childNodes();
              for(int i=0;i<CNode1.size();i++){
                  QDomElement element = CNode1.item(i).toElement();
                  if(element.tagName().compare("Class")==0){
                      qDebug()<<"name :"<<element.attribute("name")<<endl;
                      DecodeClassElement(element);
                  }else{
                      qDebug()<<"Tag name does not exist ::"<<element.tagName()<<endl;
                  }
              }
          }else{
              qWarning() << "Wrong XML" << endl;
          }
          

          }

          void xmlparsing::DecodeClassElement(QDomElement element)
          {
          QDomNodeList CNode2 = element.childNodes();
          for(int i=0;i<CNode2.length();i++){
          QDomElement ele2 = CNode2.at(i).toElement();
          qDebug()<<"Element :"<<ele2.tagName()<<endl;
          qDebug()<<"name :"<<ele2.attribute("name")<<endl;
          qDebug()<<"type :"<<ele2.attribute("type")<<endl;
          qDebug()<<"value :"<<ele2.attribute("value")<<endl;
          }
          }

          Yuvaram Aligeti
          Embedded Qt Developer
          : )

          1 Reply Last reply
          3
          • A akshay123

            I am new to Qt

            I am developing application to parse the XML file. But the File format looks like this

            <Class name="ABC">
                <LayoutEntry name="ABB"            type="class:ABB"/>
                <LayoutEntry name="ACC"            type="class:ACC"/>
                <LayoutEntry name="ADD"            type="class:ADD"/>
             </Class>
            
             <Class name="ABB">   //Declared  in class ABC
                <LayoutEntry name="ABB1"            type="class:ABB1"/>
                <LayoutEntry name="ABB2"            type="class:ABB2"/>
              </Class>
            
              <Class name="ABB1">  //Declared in class ABB
                <LayoutEntry name="ABB1"            value=ABB1"/>
              </Class>
            
              <Class name="ABB2">
                <LayoutEntry name="ABB2"            value=ABB2"/>
              </Class>
            

            Please some one suggest me for any library or method in Qt to parse this type of XML . Thanks

            Edit: Added code tags -- @Wieland

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @akshay123 As @yuvaram suggested you should first fix your XML: what you posted is not valid XML. A XML document must have exactly one root node (change Root to what ever you like):

            <Root>
            <Class name="ABC">
                <LayoutEntry name="ABB"            type="class:ABB"/>
                <LayoutEntry name="ACC"            type="class:ACC"/>
                <LayoutEntry name="ADD"            type="class:ADD"/>
             </Class>
            
             <Class name="ABB">   //Declared  in class ABC
                <LayoutEntry name="ABB1"            type="class:ABB1"/>
                <LayoutEntry name="ABB2"            type="class:ABB2"/>
              </Class>
            
              <Class name="ABB1">  //Declared in class ABB
                <LayoutEntry name="ABB1"            value=ABB1"/>
              </Class>
            
              <Class name="ABB2">
                <LayoutEntry name="ABB2"            value=ABB2"/>
              </Class>
            </Root>
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • yuvaramY Offline
              yuvaramY Offline
              yuvaram
              wrote on last edited by
              #6

              Hi @jsulm
              I have already mentioned that line to be added root element, please look again. I have added <ClassRoot> tag.

              Yuvaram Aligeti
              Embedded Qt Developer
              : )

              jsulmJ 1 Reply Last reply
              0
              • yuvaramY yuvaram

                Hi @jsulm
                I have already mentioned that line to be added root element, please look again. I have added <ClassRoot> tag.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @yuvaram I know and I wrote that in my post. I just wanted to add the whole XML document, so it is more clear what we mean.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                  Thanks @yuvaram @jsulm @yuvaram . he XML which i have posted here is not in proper format . Since the XML is very big i wanted to post a small snippet of it . Thanks for your help .

                  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