Unsolved How do we store a parsed xml file in Qt?
-
After reading the XML files using QXMLStreamReader parser I am able to read all the parent nodes, child keys and also attributes and values also. I used QXMLStreamAttributes to read attributes. Now We wanted to store the parsed XML files in a data structure. How can I do this??
Thank you
-
@varshit said in How do we store a parsed xml file in Qt?:
in a data structure
In what data structure?
-
in any Qt data structure.
-
@varshit
Hi
But what does the XML describe?
Normally you would have matching classes and add
data to them on the fly.If you just mean store the XML structure then you should have used
https://doc.qt.io/qt-5/qdomdocument.html
as that is the XML in a tree structure. -
@mrjj
HiI need to store the parsed XML file i.e the Qt parser reads the XML file and prints the output on to a console
Now I need to store that output in memory because that helps us with mapping the XML file with QML file. -
@varshit
Hi
but in what way do you need to store it ?
Like a tree so nodes have child nodes
and each node has properties and properties has name and value ? -
@varshit
One way of storing the XML is to store the very original text. Precisely because that represents the tree as deserialized text!Otherwise, as @mrjj indicated, keep the representation in
QDomDocument
/QDomNode
s.I need to store the parsed XML file i.e the Qt parser reads the XML file and prints the output on to a console
Now I need to store that output in memory because that helps us with mapping the XML file with QML file.Don't know what you mean. It only "reads the XML file and prints the output on to a console` if you make it print to the console. Separately, I don't know how it helps/is an issue related to a QML file.
-
This post is deleted! -
Hi
I am attaching a sample XML file and its output, Now I need to store this output in a data structure. for example, the data structure can be QMap, QStack or QHash or anything that has advantages.
Thank you
-
Hi
What data structure that works best would depend on what you need to do with after.
Do you need this data to be exposed to QML ?
If yes, we should use a data type that QML understand then.
Also Qmap would allow you to look up that data using a key but do you need that ? -
Hi, Thank you for helping me at this time.
yes, I need this data to be exposed to QML.
Yes, I need to store this data in keys and values style.here's where I am going wrong with what data structure should I use. can you suggest me a data structure that understands the QML?
-
@varshit said in How do we store a parsed xml file in Qt?:
e a data structure that understands the QML?
Are you trying to create a UI in QML based on that XML? If so it looks like you may want to start creating QML components as you parse the XML then
-
Hi
We want to create a UI inQML based on that XML.
We created the QML components using QComponentsNow I want to store the parsed XML file in a QMap or QHash or etc or in any container class.
Please help me with the storing part.Thank you
-
@varshit What exact problem do you have with storing key/value pairs from XML in QMap/QHash which you already print out?
-
@varshit
May I ask again why you wish to try to turn this intoQMap
orQHash
?You're going to have to store it as a "recursive tree" of
QMap
/QHash
es, to allow for the structure and avoid duplicates. For example, an XML document could easily have multiple nodes named/a/b
, so you cannot easily just use a "flat" key for your maps/hashes to avoid all the nesting....I wrote earlier that you already have a parsed structure which captures the whole XML document. And it's a
QDomDocument
withQDomNode
s. You don't have to do your own parsing/copying, because it's done for you, and you can search/walk that as required. It can also serialize to/deserialize from text. So you're done!? -
Hi
Now my main part is to store that output data (image inserted above) in [QMap<QString, QMap<QString, QString>>;] this data structure.Thank you
-
Hi
I am not able to understand or figure out how to store these Key-value pairs in the above-mentioned data structure. -
@varshit
So your top-levelQMap<QString, QMap<QString, QString>>
will have keysbreakfast_menu
,Veg
, etc. Each one will have its ownQMap<QString, QString>
as value. Those innerQMap
s will have keysmenu1
,menu2
with valuesveg
,non-veg
, etc. Nothing else to say. -
@varshit said in How do we store a parsed xml file in Qt?:
[QMap<QString, QMap<QString, QString>>;]
It would be like
using MemberMap = QMap<QString, QString>; QMap<QString, MemberMap> items; MemberMap m1; m1["text"]="value"; ... items["ID"]=m1; MemberMap m2; m1["text"]="value"; ... items["ID"]=m2;
But using your real data for key value and ID