QtScript and XML
-
Hi everybody,
I am trying to use QtScript bindings to manipulate XML documents, with no success.
I want to create and save to file a simple XML file:@var doc = new QDomDocument();
var root = doc.createElement("root");
doc.appendChild(root);
root.setAttribute("attr1", "value1")var tag = doc.createElement("tag1");
root.appendChild(tag);var file = new QFile("/tmp/test.xml");
file.open( QIODevice.WriteOnly );
var stream = new QTextStream(file);root.save(stream, 0);
file.close();
@BUT: root.save produces: <root attr1="value1"/>
doc.save produces: ""
tag.save produces: ""so, it seems that appendChild does not work.
this was also mentioned here:
http://forum.kde.org/viewtopic.php?f=117&t=88284
Any help about this?
Thanks
Davide -
When you call <code>appendChild()</code> and then later want to modify the element, I think you need to then use the new reference <code>appendChild()</code> returns, not the old one. So, for example: @var doc = new QDomDocument();
var root = doc.createElement("root");
root = doc.appendChild(root);
root.setAttribute("attr1", "value1")var tag = doc.createElement("tag1");
tag = root.appendChild(tag);var file = new QFile("/tmp/test.xml");
file.open( QIODevice.WriteOnly );
var stream = new QTextStream(file);root.save(stream, 0);
file.close(); @ -
I already tried that, but doing something like
@var root = doc.appendChild(doc.createElement("root"));
root.setAttribute("attr1", "value1")@gives a (strange!) error
TypeError: Result of expression 'root.setAttribute' [undefined] is not a function.I forgot to say, using qs_eval with this script, on ubuntu 11.04 amd64, qt 4.7.2 and libqtscript* installed.
-
Yeah: have a look at this -- @var doc = new QDomDocument();
var root = doc.createElement("root");
var newRoot = doc.appendChild(root);
if (newRoot.isNull()) print ("NULL");@ It's printing out that NULL statement, so the appendChild call is failing. And further: @root.appendChild(tag);
var hasChildren = root.hasChildNodes();
print (hasChildren);@ prints "false". This looks like a bug in the ECMAScript bindings for the Qt XML classes, but I haven't figure out where yet. -
I took a look at the generated cpp from qtscriptgenerator, and it's clearly wrong for this case. The QDomNode is being passed in by reference by the ECMAScript engine (as expected for this data type), but the code is trying to treat it as though it's been passed in by value. This actually appears to be happening throughout the QDomNode wrapper, but I have not verified that.
The generated code is: @QDomNode _q_arg0 = qscriptvalue_cast<QDomNode>(context->argument(0));
QDomNode _q_result = _q_self->appendChild(_q_arg0);
return qScriptValueFromValue(context->engine(), _q_result);@
If I change it to @QDomNode *_q_arg0 = qscriptvalue_cast<QDomNode >(context->argument(0));
QDomNode _q_result = _q_self->appendChild(_q_arg0);
return qScriptValueFromValue(context->engine(), _q_result);
@ everything then works as expected. I'm not sure where to go from here, I have no idea how qtscriptgenerator works. -
So the problem is probably in the qscriptvalue_cast type...
in this case there are many other methods that does not work (insertBefore, removeChild, ...) , in qtscript_QDomNode.cpp there are a lot of qscriptvalue_cast<QDomNode> !
I saw you already opened an issue :-)I understand that the qtscript / qtxml is not very used around the world........
-
BTW, here is the link to this issue: https://code.google.com/p/qtscriptgenerator/issues/detail?id=81