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. How to search for a tag value in xml file

How to search for a tag value in xml file

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 1.2k Views
  • 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.
  • Q Offline
    Q Offline
    QtisHard
    wrote on 17 Aug 2022, 17:38 last edited by
    #1

    one of my xml files, has

    <data>
    <feature>
    <name>Name</name>
    <value>Jacob</value>
    <age>14</age>
    </feature>
    <feature>
    <name>Name</name>
    <value>Nathan</value>
    <age>15</age>
    </feature>
    <feature>
    <name>Name</name>
    <value>Anna</value>
    <age>16</age>
    </feature>
    <feature>
    <name>Name</name>
    <value>Johnathan</value>
    <age>13</age>
    </feature>
    <data>

    I have a user taken input where they enter one of the names in the XML file.
    I store that user input in a QString variable.

    How do I find that value in the XML file?

    Example : How do I find the value of Anna in the given above XML file

    Please do help me.
    I have an approach in mind where you iterate through the XML file till you find the Value Tag, Then you compare the value stored in the value tag with the value stored in the QString variable.

    I do not know how to convert this into code format in C++

    Any resources or references or help would be welcome.
    Thank you in advance

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 Aug 2022, 17:41 last edited by
      #2

      Hi,

      Please take the time to go thought Qt's documentation. There's a whole module dedicated to xml processing.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QtisHard
        wrote on 17 Aug 2022, 17:50 last edited by
        #3

        I was able to read the file and also print all the values in all records in the given XML file.
        And I can iterate through the entire file as well by using QDomDocument and QDomElement.

        However, my task at hand is to print only the specific values related to the Name entered by the user.
        So if the user enters Anna, I want only the Name, Age and Value of Anna to be printed.

        The place where I am getting an error is that
        Because Anna is stored in the second tag in feature, I do not know how to go back and access the name tag again.

        Can you please guide me as to how I should print only the values related to what has been entered by the user?

        J 1 Reply Last reply 17 Aug 2022, 18:13
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 17 Aug 2022, 18:12 last edited by
          #4

          The brute force way is to use elementsByTagName for each of your feature nodes and if any node in the results contain the desired value, extract what you want from your feature node.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • Q QtisHard
            17 Aug 2022, 17:50

            I was able to read the file and also print all the values in all records in the given XML file.
            And I can iterate through the entire file as well by using QDomDocument and QDomElement.

            However, my task at hand is to print only the specific values related to the Name entered by the user.
            So if the user enters Anna, I want only the Name, Age and Value of Anna to be printed.

            The place where I am getting an error is that
            Because Anna is stored in the second tag in feature, I do not know how to go back and access the name tag again.

            Can you please guide me as to how I should print only the values related to what has been entered by the user?

            J Offline
            J Offline
            JonB
            wrote on 17 Aug 2022, 18:13 last edited by JonB
            #5

            @QtisHard said in How to search for a tag value in xml file:

            how to go back

            When you say that, it does not sound like you are using the QDocument? If you use that you are not reading as you go, there is no going back. For example, have you seen there is QDomNodeList QDomDocument::elementsByTagName(const QString &tagname) const? That is one simple way to search if you are looking for tags but not bothered about where they are in the structure/hierarchy. There are other ways.... [Oh, @SGaist just posted while I was typing!]

            1 Reply Last reply
            1
            • C Offline
              C Offline
              ChrisW67
              wrote on 18 Aug 2022, 00:05 last edited by
              #6

              Take a look at the Qt Xml Patterns module if you are using Qt 5.x. The Recipes Example show how you might use XQuery to find entries.

              That module was discontinued in Qt 6. You may need a third-party XML library to do the same thing in Qt 6.

              J 1 Reply Last reply 18 Aug 2022, 07:48
              0
              • C ChrisW67
                18 Aug 2022, 00:05

                Take a look at the Qt Xml Patterns module if you are using Qt 5.x. The Recipes Example show how you might use XQuery to find entries.

                That module was discontinued in Qt 6. You may need a third-party XML library to do the same thing in Qt 6.

                J Offline
                J Offline
                JonB
                wrote on 18 Aug 2022, 07:48 last edited by
                #7

                @ChrisW67 said in How to search for a tag value in xml file:

                That module was discontinued in Qt 6. You may need a third-party XML library to do the same thing in Qt 6.

                Given which, with all due respect, I do not think I would be recommending this for new users!

                @QtisHard
                While it is true you would benefit from an XQuery-alike for complex queries or large XML data, if yours is pretty straightforward you should get away with the elementsByTagName() or just writing some code to traverse the tree looking at element names for your purposes. That's nicer --- though not mandatory --- then re-parsing the text of the XML each time. Or, as you read the XML first time you might set up whatever data structures you will need for your later simple lookups, such as QMaps.

                1 Reply Last reply
                0

                7/7

                18 Aug 2022, 07:48

                • Login

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