How to search for a tag value in xml file
-
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 -
Hi,
Please take the time to go thought Qt's documentation. There's a whole module dedicated to xml processing.
-
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?
-
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.
-
@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!] -
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.
-
@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 theelementsByTagName()
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 asQMap
s.