September 4, 2011

chrisQT chrisQT
Lab Rat
5 posts

XMLHttpRequest problem

 

Hello everyone,

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

  1. onClicked: {
  2.             var doc = new XMLHttpRequest();
  3.             doc.onreadystatechange = function() {
  4.                 if (doc.readyState == XMLHttpRequest.DONE) {
  5.                     var a = doc.responseXML.documentElement;
  6.                     for (var ii = 0; ii < a.childNodes.length; ++ii) {
  7.                         console.log(a.childNodes[ii].nodeName);
  8.                     }
  9.                 }
  10.             }
  11.             doc.open("GET", "data.xml");
  12.             doc.send();
  13.         }

This is my data.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <document>
  3.     <content>
  4.         <item>
  5.             <name>A Test Name</name>
  6.             <id>1</id>
  7.         </item>
  8.     </content>
  9. </document>

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

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

Regards,
ChrisQT

3 replies

September 5, 2011

mbrasser mbrasser
Lab Rat
452 posts

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.

  1. <document><content></content></document>

Regards,
Michael

September 5, 2011

chrisQT chrisQT
Lab Rat
5 posts

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

Regards,
chrisQT

September 6, 2011

mbrasser mbrasser
Lab Rat
452 posts

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]

 
  ‹‹ Repeater is not refreshed after changing model      Send the signal once, but recived twice. ››

You must log in to post a reply. Not a member yet? Register here!