[NEWBIE] Parsing XML to strings
-
or even better
@
QT_BEGIN_NAMESPACE
class QXmlStreamReader;
QT_END_NAMESPACE
@ -
Ok, I kept "#include <QXmlStreamReader>" because using "class QXmlStreamReader;" broke everything. And... Why would you prefer that the class definition was used?
As I see it, from my unexperienced view, you define a class called QXmlStreamReader. Not necessarily what I want to use, which is the streaming functionality... Or does it serve the same purpose?
And in what way is using #include inferior to defining the class?Anyway... When I made the same declarations in the header, I got these errors:
@../xml-interpret/xmlinterpret.cpp:38:54: error: invalid use of void expression@
They appear for all the times I refer to other functions, instead of "no function for call to".
This is a piece of code where the error occurs:
@gameName.append(this->parseTitle(xml));@
It's in the if-stack. What have I done wrong this time? -
@this->parseTitle(xml);@
does not return anything and you can therefore not append it to gameName.
I suggest you read up on the basics of C++ before continuing.[quote]
Ok, I kept “#include <QXmlStreamReader>” because using “class QXmlStreamReader;” broke everything. And… Why would you prefer that the class definition was used?
As I see it, from my unexperienced view, you define a class called QXmlStreamReader. Not necessarily what I want to use, which is the streaming functionality… Or does it serve the same purpose?
And in what way is using #include inferior to defining the class?
[/quote]
It's called forward declaration and speeds up compilation. The #include <QXmlStreamReader> should go in the .cpp file.
"On when to use forward declaration":http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml -
[quote author="loladiro" date="1313352748"]
I suggest you read up on the basics of C++ before continuing.
[/quote]
Now that was a very helpful suggestion. (I mean it.) My C++ is as good as rusted away/I don't know much C++.
I'm sorry for bothering all of you with this, but I'll be back in this thread when I understand more of how C++ works. -
[quote author="HowyHappy" date="1313354049"]
[quote author="loladiro" date="1313352748"]
I suggest you read up on the basics of C++ before continuing.
[/quote]
Now that was a very helpful suggestion. (I mean it.) My C++ is as good as rusted away/I don't know much C++.
I'm sorry for bothering all of you with this, but I'll be back in this thread when I understand more of how C++ works.[/quote]No problem. We will be more than glad to help when you're back :) .
-
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.