Compare Revisions

Connecting to Remote Content

Change Revisions

Revision 94729:

Revision 94729 by jpgtama on

Revision 94730:

Revision 94730 by JamesWatts on

Title:
Connecting to Remote Content
Connecting to Remote Content
Slug:
XUL_School/Connecting_to_Remote_Content
XUL_School/Connecting_to_Remote_Content
Content:

Revision 94729
Revision 94730
n20let request =n
21  Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].20let request = Components.classes["@mozilla.org/xmlextras/xmlhttpr
 >equest;1"].
22    createInstance(Ci.nsIXMLHttpRequest);21    createInstance(Components.interfaces.nsIXMLHttpRequest);
23 22request.onload = function(aEvent) {
24request.onload =
25  function(aEvent) {
n27  };n24};
28request.onerror =25request.onerror = function(aEvent) {
29  function(aEvent) {
n31  };n27};
n60request.setRequestHeader(n
61    "Content-Type", "application/x-www-form-urlencoded");56request.setRequestHeader("Content-Type", "application/x-www-form-
 >urlencoded");
n94{ "shops": [{ "name": "Apple", "code": "A001"}, {"name": "Orange"n89{"shops": [{"name": "Apple", "code": "A001"}, {"name": "Orange"}]
>}], "total": 100 }>, "total": 100}
n105  let text = aEvent.target.responseText;n100    let text = aEvent.target.responseText;
106  let jsObject = JSON.parse(text);101    let jsObject = JSON.parse(text);
n108  window.alert(jsObject.shops[1].name);  // => "Orange"n103    window.alert(jsObject.shops[1].name); // => "Orange"
109  window.alert(jsObject.total);  // => 2;104    window.alert(jsObject.total);         // => 2;
n141     <span class="code-tag">&lt;name&gt;</span>Orange<span class=n136      <span class="code-tag">&lt;name&gt;</span>Orange<span class
>"code-tag">&lt;/name&gt;</span>>="code-tag">&lt;/name&gt;</span>
n156  let responseXML = aEvent.target.responseXML;n151    let responseXML = aEvent.target.responseXML;
157  let rootElement = responseXML.documentElement;152    let rootElement = responseXML.documentElement;
n159  if (rootElement &amp;&amp;n
160      "parseerror" != rootElement.tagName) {154    if (rootElement &amp;&amp; "parseerror" != rootElement.tagNam
 >e) {
161    let shopElements = rootElement.getElementsByTagName("shop");155        let shopElements = rootElement.getElementsByTagName("shop
 >");
162    let totalElement = rootElement.getElementsByTagName("total")[156        let totalElement = rootElement.getElementsByTagName("tota
>0];>l")[0];
n164    window.alert(n
165      shopElements[1].getElementsByTagName("name")[0].firstChild.158        window.alert(shopElements[1].getElementsByTagName("name")
>nodeValue); // =&gt; Orange>[0].firstChild.nodeValue); // =&gt; Orange
166    window.alert(159        window.alert(totalElement.firstChild.nodeValue);         
 >                            // =&gt; 2
167      totalElement.firstChild.nodeValue); // =&gt; 2
168  }160    }
n220         <span class="code-tag">&lt;/xul:hbox&gt;</span>n212        <span class="code-tag">&lt;/xul:hbox&gt;</span>
n240let domParser =n232let domParser = Components.classes["@mozilla.org/xmlextras/dompar
 >ser;1"].
241  Cc["@mozilla.org/xmlextras/domparser;1"].
242    createInstance(Ci.nsIDOMParser);233    createInstance(Components.interfaces.nsIDOMParser);
243let fileStream =234let fileStream = Components.classes["@mozilla.org/network/file-in
 >put-stream;1"].
244  Cc["@mozilla.org/network/file-input-stream;1"].
245    createInstance(Ci.nsIFileInputStream);235    createInstance(Components.interfaces.nsIFileInputStream);
246let xsltProcessor =236let xsltProcessor = Components.classes["@mozilla.org/document-tra
 >nsformer;1?type=xslt"].
247  Cc["@mozilla.org/document-transformer;1?type=xslt"].
248    createInstance(Ci.nsIXSLTProcessor);237    createInstance(Components.interfaces.nsIXSLTProcessor);
n254xslDocument =n
255  domParser.parseFromStream(243xslDocument = domParser.parseFromStream(
n270  let responseXML = aEvent.target.responseXML;n258    let responseXML = aEvent.target.responseXML;
271  let xulNode;259    let xulNode;
n273  // transform the XML document to a XUL documentn261    // transform the XML document to a XUL document
274  xulDocument = xsltProcessor.transformToDocument(responseXML);262    xulDocument = xsltProcessor.transformToDocument(responseXML);
t276  // append the XUL node to a XUL elementt264    // append the XUL node to a XUL element
277  xulNode = document.adoptNode(xulDocument.firstChild);265    xulNode = document.adoptNode(xulDocument.firstChild);
278  document.getElementById("foo").appendChild(xulNode);266    document.getElementById("foo").appendChild(xulNode);

Back to History