[NEWBIE] Parsing XML to strings
-
Wow, quickly reading through the basics helped a lot. Now, I have the initial Xml parsed to strings, and I'm ready to proceed with building DOM-trees.
...When I learn how to build them.
I must say: QXmlStreamReader is simple to use once you understand how it works. -
[quote author="HowyHappy" date="1313435556"]Now, I have the initial Xml parsed to strings, and I'm ready to proceed with building DOM-trees.[/quote]
If you're wanting a DOM tree, perhaps your best bet would be simply using "QDomDocument":http://doc.qt.nokia.com/latest/qdomdocument.html rather than building one from scratch with strings read from an QXmlStreamReader.
But, it all depends on your use case.
-
Well, of course, I won't use QXmlStreamReader to build a DOM tree. It wouldn't be sufficient for the amounts of data this program should be able to handle. In this usecase, I want there to be indexes for different kinds of objects that can be pulled easily into a renderer and other components. So, f.ex., you'd be able to pull trees of all sorts, and it will load the different properties of it like texture and model without further fuzz.
For this purpose, a QDomDocument will do perfectly.
I have all the features and ways of doing it planned well. The thing missing is writing it. -
Ok... I was just making sure you knew the options. Since you'd self-proclaimed yourself as a "newbie" then there was a little room for interpretation (and guessing) about what you knew about and what you didn't. :-)
Good luck with your coding!
-
I think I've made some progress, but so far I've taken it after my own perception of what does what.
This is the code:
@void LaunchPad::parseActors() {
//Open the file and assign it to the document
QFile actorFile(globalActorsFile);
actorFile.open(QIODevice::ReadOnly | QIODevice::Text);
actorIndex.setContent(&actorFile);//Start building the DOM tree QDomElement actorElement; QDomNodeList actors = actorElement.elementsByTagName("sysName");
}@
Will this build me a usable DOM tree?
This is an example of the target document:
@<actors>
<actor name="hero" sysName="hero">
<model>act_hero_001.zact</model>
<texture>act_hero_001.ztex</texture>
</actor>
<actor name="Princess" sysName="princess">
<type>NPC</type>
<model>act_princess_001.zact</model>
<texture>act_princess_001.ztex</texture>
<talk>true</talk>
<logic>act_princess.zlgc</logic>
</actor>
</actors>@
Would I be able to fetch all the child nodes of the actor called "hero"?
Or am I only halfway?And I'm also trying to draw a test-window, just to see that the text gets parsed. So I put in this function:
@void LaunchPad::drawTestWindow() {
QWidget titleWindow;
titleWindow.setWindowTitle(gameName);
titleWindow.show();
}@
But it doesn't seem to work well so far.
I get this weird error, it says:
@/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../crt1.o: In function_start': (.text+0x20): undefined reference to
main'
collect2: ld returned 1 exit status@
I bet that I've forgotten to enter something that is required by QWidget or displaying something in general..? -
Where is your main()? Looks like the linker can't find it.
-
I'm having a problem with main()...
I don't know how to use it.
I've written this so far:
@#include <QtGui/QApplication>
#include <QWidget>#include "xmlinterpret.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);return app.exec();
}
@
But I don't understand how to make it show the widget I configured...
I assume that I have to use a pointer somewhere? -
@
#include <QtGui/QApplication>
#include "somewidget.h"int main(int argc, char *argv[])
{
QApplication app(argc, argv);SomeWidget sw; sw.show(); return app.exec();
}
@or just look at pretty much any of the examples in the docs.
-
I've successfully implemented the int main function now. I chose to put it in the same file, because then I won't have to use pointers, and this is basicly the launchpad for the engine where it all begins.
Ok, new problem:
The program is nicely written now, but I'm not entirely sure on how to use QFile.
So far, I've used it this way:
@QFile xmlFile;
xmlFile.setFileName("game.xml");@
And:
@QFile actorFile(globalActorsFile);@
I also set QDir to the current directory in main() like so:
@QDir::setCurrent(".");@
But when I try to start the program, I get "QFSFileEngine::open: No file name specified".
What have I done wrong? -
Relative paths are resolved relative to the current directory. The dot "." represents the current directory. So our setCurrent is nothing else than "change the current directory to the current directory".
I guess you want to set the current directory to that folder that holds the executable. If so, then use this:
@
QDir::setCurrent(qApp->applicationDirPath());
@Or better prepend your file path with that string.