Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. QtScript and XML
Forum Updated to NodeBB v4.3 + New Features

QtScript and XML

Scheduled Pinned Locked Moved Language Bindings
7 Posts 3 Posters 5.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davidecapod000
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris H
      wrote on last edited by
      #2

      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(); @

      1 Reply Last reply
      0
      • D Offline
        D Offline
        davidecapod000
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris H
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris H
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              davidecapod000
              wrote on last edited by
              #6

              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........

              1 Reply Last reply
              0
              • F Offline
                F Offline
                Frank Thomas
                wrote on last edited by
                #7

                BTW, here is the link to this issue: https://code.google.com/p/qtscriptgenerator/issues/detail?id=81

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved