I have made some changes to my jhs installation to allow the retrieval 
from the server of xml files and xml-tagged J nouns (to a xul app). The 
diff output is below. Using these facilities allows me to serve more 
data than jijx will output to the screen;  use XPath facilities in 
javascript to extract data from the J result; and, when I get to it, tag 
a J noun with structure information to allow boxed nouns to be passed 
between J and javascript code. I apologize for the length of what follows.

To retrieve an existing .xml file
    http://127.0.0.1:50000/theXMLFILENAME

To execute a jsentence that returns an xml-tagged noun,
    http://127.0.0.1:50000/jxmlout

An illustration:
 var url="http://127.0.0.1:50000/jxmlout";;
 var req = new XMLHttpRequest();
 req.open('POST', url, true);
.
.
.
req.send("jsentence="+myEncodeURIComponent(sentence)+"&jajax=");

In the "onreadystatechange" function I call req.responseXML which 
returns a "dom." In my (very limited experience) this dom is either: 1) 
what I want; or, 2) an error structure the J code creates; or 3) a 
"parse error" from XMLHttpRequest. An example of handling the response is:

 var dom=this.jhsResponseXML;
 var sMsg="";
 if (dom.documentElement.nodeName == "parsererror")
   sMsg="error while parsing replay";
 else if(dom.documentElement.nodeName == "error") {
   sMsg=dom.firstChild.textContent+"\n";
   sMsg+=dom.documentElement.lastChild.textContent;
 } else if(1) {
   var results = this.evaluateXPath(dom, this.xpath.value);
   sMsg="Car#\tid\n";
   for (let i in results) {
     sMsg+=i + "   \t" + results[i].value+"\n";
   }   
}
alert(sMsg);

References: google for: mdc "parsing xml" and mdc evaluateXPath
===========================================
The diff file:
===========================================
diff -a -c -N jhs.org/core.ijs jhs/core.ijs
*** jhs.org/core.ijs    2010-05-03 12:54:16.000000000 -0500
--- jhs/core.ijs    2010-05-03 14:30:55.000000000 -0500
***************
*** 110,115 ****
--- 110,116 ----
  if. 1=NVDEBUG do. smoutput NV end. NB. HNV,NV
  DISPLAY=: 0
  if.     0~:$       r=. getvqq'jbutton'                do.
+ elseif. +./'jxmlout' E. URL [XMLS=:getv'jsentence' 
[r=.'url',METHOD,'_',URL,'_ XMLS_jfe_'   do.
  elseif. -.0-:      r=. getv'jsentence'              do. DISPLAY=: 1
  elseif. -.'.' e. URL [r=.'url',METHOD,'_',URL,'_ 0'    do.
  elseif. 1          [r=. 'getsrcfile_jfile_ URL_jfe_' do.
***************
*** 684,687 ****
  load__ '~system\extras\util\jhs\jijxmin.ijs'
  load__ '~system\extras\util\jhs\jal.ijs'
  load__ '~system\extras\util\jhs\jdemo.ijs'
!
--- 685,688 ----
  load__ '~system\extras\util\jhs\jijxmin.ijs'
  load__ '~system\extras\util\jhs\jal.ijs'
  load__ '~system\extras\util\jhs\jdemo.ijs'
! load__ '~system\extras\util\jhs\jxmlout.ijs'
diff -a -c -N jhs.org/jfile.ijs jhs/jfile.ijs
*** jhs.org/jfile.ijs    2010-05-01 14:44:00.000000000 -0500
--- jhs/jfile.ijs    2010-05-01 23:16:51.000000000 -0500
***************
*** 432,442 ****
--- 432,469 ----
   'text/x-component'gsrcf y
  elseif. '.swf'-:_4{.y do.
   'application/x-shockwave-flash'gsrcf y
+ elseif. +./'.xml' E. y do.
+  xmlget y
  elseif. 1 do.
   smoutput 'do not get files of type: ',y
  end.
  )
 
+ xmlhead=: 0 : 0
+ HTTP/1.1 200 OK
+ Accept-Ranges: bytes
+ Content-Type: application/xml
+ Content-Length: LENGTH
+
+ )
+
+ xml404=: 0 : 0
+ <?xml version="1.0" encoding="iso-8859-1"?>
+ <error><msg>MSG</msg><txt>TXT</txt></error>
+ )
+
+ NB. return xml file
+ xmlget=: 3 : 0
+ try.
+  d=. 1!:1 <jpath LF -.~ y    NB. javascript strings LF terminated?
+ catch.
+  et=. ><;._1 ('<';'&lt;';'>';'&gt;';LF;'') rplc~ [13!:12 ''
+  msg=. {.et [txt=. {:et
+  d=. deb xml404 rplc ('MSG';msg;'TXT';txt)
+ end.
+  htmlresponse d,~xmlhead rplc 'LENGTH';":#d
+ )
+
  favicon=: 3 : 0
  htmlresponse htmlfav,1!:1 <jpath'~bin\icons\favicon.ico'
  )
diff -a -c -N jhs.org/jxmlout.ijs jhs/jxmlout.ijs
*** jhs.org/jxmlout.ijs    1969-12-31 18:00:00.000000000 -0600
--- jhs/jxmlout.ijs    2010-05-03 15:30:24.000000000 -0500
***************
*** 0 ****
--- 1,38 ----
+ NB. J HTTP Server - jxmlout service
+
+ coclass'jxmlout'
+ coinsert'jfe'
+
+ urlget=: mtv
+ NB. urlpost=: mtv
+
+ urlpost=: 3 : 0
+ xmlget y
+ )
+
+ xmlhead=: 0 : 0
+ HTTP/1.1 200 OK
+ Accept-Ranges: bytes
+ Content-Type: application/xml
+ Content-Length: LENGTH
+
+ )
+
+ xml404=: 0 : 0
+ <?xml version="1.0" encoding="iso-8859-1"?>
+ <error><msg>MSG</msg><txt>TXT</txt></error>
+ )
+
+ NB. return xml response
+ NB. y is a jsentence
+ xmlget=: 3 : 0
+ try.
+  d=. ". y   
+ catch.
+  et=. ><;._1 ('<';'&lt;';'>';'&gt;';LF;'') rplc~ [13!:12 ''
+  msg=. {.et [txt=. {:et
+  d=. deb xml404 rplc ('MSG';msg;'TXT';txt)
+ end.
+  htmlresponse d,~xmlhead rplc 'LENGTH';":#d
+ )
+
Common subdirectories: jhs.org/src and jhs/src


----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to