XMLHttpRequest problem



  • Hello everyone,

    experimenting with fresh install of QTSDK 1.1.3.
    I have this JS, which gets executed, on a mouse click:

    @onClicked: {
    var doc = new XMLHttpRequest();
    doc.onreadystatechange = function() {
    if (doc.readyState == XMLHttpRequest.DONE) {
    var a = doc.responseXML.documentElement;
    for (var ii = 0; ii < a.childNodes.length; ++ii) {
    console.log(a.childNodes[ii].nodeName);
    }
    }
    }
    doc.open("GET", "data.xml");
    doc.send();
    }@

    This is my data.xml

    @<?xml version="1.0" encoding="UTF-8"?>
    <document>
    <content>
    <item>
    <name>A Test Name</name>
    <id>1</id>
    </item>
    </content>
    </document>@

    This gives me this output on console:
    #text
    content
    #text

    Do I miss something? Any ideas, where this #text comes from?

    Regards,
    ChrisQT



  • As far as I understand the "extra" text nodes are caused by whitespace (spaces, newlines, etc). For example, the following file should only print "content" when traversing the DOM as you do above.

    @
    <document><content></content></document>
    @

    Regards,
    Michael



  • Thanks, that did it. So I have to escape my whole (valid) document to make it QML/JS valid?

    Regards,
    chrisQT



  • From what I can see whitespace #text nodes seem to be a common issue (e.g. http://stackoverflow.com/questions/1898640/ignoring-empty-xml-nodes-in-javascript ), so you may be able to find some helpful ways of dealing with them by searching around a bit.

    Regards,
    Michael

    [EDIT: fixed link, Volker]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.