This forum has been archived. All content is frozen. Please use KDE Discuss instead.

QTScript & XML

Tags: None
(comma "," separated)
De_Dood
Registered Member
Posts
18
Karma
0
OS

QTScript & XML

Thu Jun 03, 2010 7:57 pm
Hi all,

I'v been trying to create a config file for my Amarok script with XML (QDomElement). I based my script on an C++ sample. However, I only get an empty XML tree. The code below is my example.

Code: Select all
Importer.loadQtBinding("qt.core");
Importer.loadQtBinding("qt.xml");

var doc = new QDomDocument("config");
var root = new QDomElement("config");
doc.appendChild(root);
Amarok.alert(doc.toString());


Can somebody tell me what I'm doing wrong?

Tim
De_Dood
Registered Member
Posts
18
Karma
0
OS

Re: QTScript & XML

Tue Jun 08, 2010 10:42 am
Is there no-one who can help me with this?
Or do I have to ask my question somewhere else?
User avatar
marcel
Registered Member
Posts
595
Karma
0
OS

Re: QTScript & XML

Tue Jun 08, 2010 12:33 pm
I do not know anything about these scripts, but I would guess that the file is not found and that is why you get back an empty parse tree. You should provide a valid path, not only the file name.
De_Dood
Registered Member
Posts
18
Karma
0
OS

Re: QTScript & XML

Tue Jun 08, 2010 12:57 pm
Actualy, the small demo code I provide is not yet meant to write to (or read from) a file, but to create the XML tree in memory and the show the generated code in a message box.

So if the had code workd, it shoold create in memory a XML tree containing only one node. And when the message box appears it shoold show somthing like this:
"<config></config>".
But it doesn't work and it only displays an empty string. So I'm wondering why.
jmrk
Registered Member
Posts
13
Karma
0
OS

Re: QTScript & XML

Tue Jun 08, 2010 9:46 pm
You can't construct QDomElements that way. Use QDomDocument.createElement(tagname).

For more info, see: http://doc.trolltech.com/4.6/qdomdocument.html as well as related classes: http://doc.trolltech.com/4.6/classes.html. That documentation is for C++, but if you disregard variable types most of it applies to QtScript as well.
De_Dood
Registered Member
Posts
18
Karma
0
OS

Re: QTScript & XML

Wed Jun 09, 2010 4:20 pm
Hi jmrk,

I tried to translate the C++ example in [url]http://doc.trolltech.com/4.6/qdomdocument.html[/ur] to QtScript but my string is still empty.

This is the original code:
Code: Select all
QDomDocument doc("MyML");
 QDomElement root = doc.createElement("MyML");
 doc.appendChild(root);

 QDomElement tag = doc.createElement("Greeting");
 root.appendChild(tag);

 QDomText t = doc.createTextNode("Hello World");
 tag.appendChild(t);

 QString xml = doc.toString();


My translated code:
Code: Select all
Importer.loadQtBinding("qt.core");
Importer.loadQtBinding("qt.xml");

var doc = new QDomDocument("MyML");
var root = doc.createElement("MyML");
doc.appendChild(root);

var tag = doc.createElement("Greeting");
root.appendChild(tag);
 
var t = doc.createTextNode("Hello World");
tag.appendChild(t);

var xml = doc.toString();
 
Amarok.alert(xml);
jmrk
Registered Member
Posts
13
Karma
0
OS

Re: QTScript & XML

Thu Jun 10, 2010 10:49 am
hm... sorry, no idea what's wrong now.

I don't see any mistakes in your translation to QtScript. But I can confirm that it doesn't work as expected. It looks as if the .toString() method simply didn't care about anything you inserted using .appendChild(), but as far as I understand it, that's not the intended behavior of .toString().

In C++, the example works, so this might be a bug somewhere in the QtScript framework, or a QtScript related peculiarity that I have no clue about.
De_Dood
Registered Member
Posts
18
Karma
0
OS

Re: QTScript & XML

Thu Jun 10, 2010 7:15 pm
Well, actually, I don't think the toString() function is the problem. Because when I load an XML file the toString() function works without any problems.
It's more like the appendChild function doesn't work. Because hen you do doc.hasChildNodes() it returns false.

At the moment I solved it the old fashion way by building a string and writing that to a file but it would be nice to just have it work.
User avatar
marcel
Registered Member
Posts
595
Karma
0
OS

Re: QTScript & XML

Fri Jun 11, 2010 9:54 am
Is there a return value for appendChild? You might want to check this and narrow down where exactly the problem is caused.
If this is a bug, you may want to file a bug report to get it fixed.


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], Sogou [Bot]