??xml version="1.0" encoding="utf-8" standalone="yes"?>精品亚洲AV无码一区二区,国产精品亚洲综合一区,亚洲精品无码成人http://www.tkk7.com/JRobot/category/2763.htmljava相关技?eclipse,swt,jfacezh-cnTue, 27 Feb 2007 08:40:02 GMTTue, 27 Feb 2007 08:40:02 GMT60XAJAX ~程入门实例两则http://www.tkk7.com/JRobot/articles/69490.htmlJRobotJRobotWed, 13 Sep 2006 14:25:00 GMThttp://www.tkk7.com/JRobot/articles/69490.htmlhttp://www.tkk7.com/JRobot/comments/69490.htmlhttp://www.tkk7.com/JRobot/articles/69490.html#Feedback0http://www.tkk7.com/JRobot/comments/commentRss/69490.htmlhttp://www.tkk7.com/JRobot/services/trackbacks/69490.html 作?snow 日期:2006-01-25 XAJAX是一个比较优U的AJAX toolkit工具Q安装很单,是吧他的xajax.inc.php文gcopyC制定的目录下面就可以了?br />下面举两个列子来说明如何使用

1Qhelloworld
helloworld是每一个编E语a所必须的,所以这里也不例外,我会(x)׃码给?br />


<?php
// helloworld.php demonstrates a very basic xajax implementation
// using xajax version 0.1 beta4
//
http://xajax.sourceforge.n...

require ('xajax.inc.php');  #必须的,攑ֈ脚本的最前面

#调用函数Q该函数被javascript调用
function helloWorld($isCaps)
{
if ($isCaps)
$text = "HELLO WORLD!";
else
$text = "Hello World!";

$objResponse = new xajaxResponse();
$objResponse->addAssign("div1","innerHTML",$text); #lid为div1的html元素的innerHTML属性分?text指,其他的方法请参考前面脓(chung)出的XAJAX介绍

return $objResponse->getXML();   #q回赋值后的代码,采用xml格式
}

// Instantiate the xajax object. No parameters defaults requestURI to this page, method to POST, and debug to off
$xajax = new xajax();

//$xajax->debugOn(); // Uncomment this line to turn debugging on

// Specify the PHP functions to wrap. The JavaScript wrappers will be named xajax_functionname
$xajax->registerFunction("helloWorld"); #注册函数Q这个也是必ȝQ用来保证,javascript能调用php里面的函?br />// Process any requests. Because our requestURI is the same as our html page,
// this must be called before any headers or HTML output have been sent
$xajax->processRequests();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtm....
<html>
<head>
<title>xajax example</title>
<?php
#输出javascript脚本Q这个方法必L在head之间Q特别是应该攑֜<html>标签之前Q否则无法工l?br />$xajax->printJavascript();
?>
</head>
<body style="text-align:center;">
<div id="div1" name="div1">&#160;</div>
<br/>

<button onclick="xajax_helloWorld(0)" >Click Me</button>
<button onclick="xajax_helloWorld(1)" >CLICK ME</button>
<script type="text/javascript">
xajax_helloWorld(0); // call the helloWorld function to populate the div on load
</script>
</body>
</html>


上面的脚本是一个简单的例子Q但是体CXAJAX使用的一个流E。需要注意的是在html脚本中,我们调用的函数名?br />xajax_helloWorld (),但是我们~程和注册的函数名却是helloWorldQ这不是一个错误,而是必须q样做,你可以查看xajax.inc.php的代码就知道Q他? 认的在html调用的函数名前缀是xajax_helloWorld()Q因此如果你不修改xajax.inc.phpQ那么你在html里面调用函数 必d上xajax_q样的前~?br />
E序演示

2Q表单处?br />q个例子是我花时间比较多的,也是大家比较兛_的,虽然官方也给Z一个form的例子,但是对表单数据的处理都是自己再重新写的函敎ͼ而没有用xajax.getFormValues函数Q个得比较复杂。下面是官方的form演示和源代码

form演示

form源代?/a>

下面是我的代?br />
<?php
require("includes/xajax.inc.php");
function doSomething( $formData )
{
$objResponse = new xajaxResponse();
$str="";
foreach($formData as $key=>$value)
if (empty($str))
$str.=$key."='".$value."'";
else
$str.=",".$key."='".$value."'";
//$str="form data is ".implode(",",$formData);
//$objResponse->addAlert(print_r($formData));
$objResponse->addAssign("result", "innerHTML", $str );
return $objResponse->getXML();
}

function hb()
{
$objResponse = new xajaxResponse();
$objResponse->addAssign("doIt", "value", "Working...");
$objResponse->addAssign("doIt", "disabled", true);
$objResponse->addScript("xajax_doSomething(xajax.getFormValues('someForm'));");

return $objResponse->getXML();
}


?>
<?php
$xajax = new xajax();
//$xajax->debugOn();
$xajax->registerFunction("doSomething");
$xajax->registerFunction("hb");
$xajax->processRequests();
?>
<HTML>
<HEAD>
<?php $xajax->printJavascript(); ?>
</HEAD>

<BODY>
<form id='someForm'>
<table>
<tr>
<td>username:
<input type="text" name='user' id='user' />
</td>
<td>password:<input type="password" id='pwd' name='pwd' /></td>
<td><input type='button' id='doIt' value='Do It' onClick="xajax_hb()"></input>
</td></tr>
</table>
</form>

<div id='result'>
</div>
</BODY>
</HTML>


q个要注意的比较?br />aQ表单的元素必须有name属性,q个一定要注意Q这是我痛苦了一天得到的l果Q否则getFormValues函数得不到表单数据?br />b) doSomething($formData)函数中的参数$formData是一个数l元素,索引是表单元素的名称Q属性name的|Q其值是表单元素的|属性value的|
cQ?如果debugon打开了,可能?x)出现死循环的警告窗口,目前q不知道原因?/font>

JRobot 2006-09-13 22:25 发表评论
]]>
xajax基础心得http://www.tkk7.com/JRobot/articles/69489.htmlJRobotJRobotWed, 13 Sep 2006 14:22:00 GMThttp://www.tkk7.com/JRobot/articles/69489.htmlhttp://www.tkk7.com/JRobot/comments/69489.htmlhttp://www.tkk7.com/JRobot/articles/69489.html#Feedback0http://www.tkk7.com/JRobot/comments/commentRss/69489.htmlhttp://www.tkk7.com/JRobot/services/trackbacks/69489.html

转自Qhttp://spaces.msn.com/blackwoodk ... 2wpZwYtrg!169.entry

最q研I了一下ajaxQ最后选了xajax作ؓ(f)基本class
参考站点:(x)http://xajax.sourceforge.net

因ؓ(f)基本都是gb2312所以把xajax.inc.php里面的编码改了一下,不过老实说UTF-8不错Q?
$this->xml = "<?xml version="1.0" encoding="GB2312"?>";
重新装了一下KaneXajaxc,
除此以外基本没怎么改动?

<?php
require_once(‘Smarty/Smarty.class.php?;
require_once(‘Xajax/xajax.inc.php?;
function myFunction($arg)
{
$objResponse = new xajaxResponse();
$objResponse->addAssign("SomeElementId","innerHTML", $newContent);
return $objResponse->getXML();
}
$Kanexajax = new KaneXajax();
$Kanexajax->registerFunction("myFunction");
$Kanexajax->processRequests();
$KaneSMARTY = new KaneSmarty();
$KaneSMARTY->assign(‘xajax_javascript? $xajax->getJavascript());
$KaneSMARTY->display(myTEMPLATE);
?>
一个最基本的phpok了,当然tpl里面<{$xajax_javascript}>要加?lt;HEAD></HEAD>之间

再说说基本的Ҏ(gu)吧:(x)

* addAssign($sTargetId,$sAttribute,$sData)
讄$sTargetId元素?sAttribute属性ؓ(f)$sDataQ常用的有div的innerHTMLQinput的value什么的?

$objResponse->addAssign("contentDiv","innerHTML","Some Text");
$objResponse->addAssign("checkBox1","checked","true");
* addAppend($sTargetId,$sAttribute,$sData)
q加$sData?sTargetId元素?sAttribute属性上

$objResponse->addAppend("contentDiv","innerHTML","Some Text");
* addPrepend($sTargetId,$sAttribute,$sData)
预制$sTargetId元素?sAttribute属性ؓ(f)$sDataQ老实_(d)没在实战中用q呢Q呵?

$objResponse->addPrepend("contentDiv","innerHTML","Some Text");
* addReplace($sTargetId,$sAttribute,$sSearch,$sData)
替换$sTargetId元素?sAttribute属性中?sSearch?sData?

$objResponse->addReplace("contentDiv","innerHTML","text","<strong>text</strong>");
* addClear($sTargetId,$sAttribute)
清空$sTargetId元素?sAttribute属?

$objResponse->addClear("Input1","value");
* addCreate($sParentId, $sTagname, $sId, $sType)
创徏$sParentId元素的一个新的子元素Q其name?sTagnameQId?sIdQ类型ؓ(f)$sType

$objResponse->addCreate("form1","input", "pass", "password");
* addRemove($sElementId)
U除$sElementId元素

$objResponse->addRemove("div1");
* addAlert($sMsg)
弹出一个内容ؓ(f)$sMsg的提C框

$objResponse->addAlert("This is some text");
* addScript($sJS)
执行一Djavascript

$objResponse->addScript("var txt = prompt(‘get some text?;");
$objResponse->addScript("window.location=""."demo.php?id=1"."";");
q两天用PHP+SMARTY+XAJAX写了一些小东西Q感觉上是调试完全靠感觉,不过
按照DB的debugQSmarty的debugQxajax的debugOn()Q的步骤Q一步步地测好再往下写?x)好很?
因ؓ(f)Qsmarty已经够敏感,l常l你一张大白脸Q而xajax更加敏感QQ何的地方有一点小错误Q他立刻完蛋Q这L(fng)女h娶回家真是挺可怕的Q不q谁让她支持Smarty呢?

q两天再把改好的一些代码脓(chung)上来吧,感觉q有的优?/div>

JRobot 2006-09-13 22:22 发表评论
]]>十分钟学?xajax http://www.tkk7.com/JRobot/articles/69425.htmlJRobotJRobotWed, 13 Sep 2006 09:38:00 GMThttp://www.tkk7.com/JRobot/articles/69425.htmlhttp://www.tkk7.com/JRobot/comments/69425.htmlhttp://www.tkk7.com/JRobot/articles/69425.html#Feedback0http://www.tkk7.com/JRobot/comments/commentRss/69425.htmlhttp://www.tkk7.com/JRobot/services/trackbacks/69425.html 译者按: xajax 最大的特点是他采用了xml responseQ这h们可以用php来布|,处理异步传送数据之后,|页内容的更新。而这些操作其它的ajax 框架都是由js来完成的的。xajax 使我们只需要写一些php函数Q就可以实现?br />所有学好xajax的关健在于熟l掌?xajaxresponse cR?br />


tutorials:learn xajax in 10 minutes
教程:十分钟学?xajax


using xajax in a php script

一个用的xajax的php脚本:


include the xajax class library:

调用xajaxcd:


require_once ( " xajax.inc.php " ); 


instantiate the xajax object:

实例化xajax对象


$xajax   =   new  xajax(); 


register the names of the php functions you want to be able to call through xajax:

注册一个你想用xajax来调用的php函数?与javascript中的函数名相对应 xajax_myfunction)


$xajax -> registerfunction( " myfunction " ); 


write the php functions you have registered and use the xajaxresponse object to return xml commands from them:

~写那个你刚刚已l注册的php函数Qƈ从中?xajaxresponse 对象来返回xml指o(h)?/p>

function  myfunction( $arg )
{
    
//  do some stuff based on $arg like query data from a database and
    // put it into a variable like $newcontent
    //对参?arg做一些诸如:(x)从数据库中获取数据后定义l?newcontent 变量的基本操?br />    
    // instantiate the xajaxresponse object
    //实例化 xajaxresponse 对象

     $objresponse   =   new  xajaxresponse();
    
    
//  add a command to the response to assign the innerhtml attribute of
    // the element with id="someelementid" to whatever the new content is
    // 在响应实例中d一个命令,用来id为someelementid的innerhtml元素属?br />    // 变ؓ(f)M新的内容.

     $objresponse -> addassign( " someelementid " , " innerhtml " ,   $newcontent );
    
    
// return the xml response generated by the xajaxresponse object
    //q回由 xajaxresponse 对象所生成的xml 响应

     return   $objresponse -> getxml();


before your script sends any output, have xajax handle any requests:

在你脚本传送出M东西?xajax都要处理所有请?/p>

$xajax -> processrequests(); 


between your <head></head> tags, tell xajax to generate the necessary javascript:

在该늚<head>?lt;/head>标签之间插入下列代码Qxajax实例可以自己生成所必需的js


<? php  $xajax -> printjavascript();  ?>


call the function from a javascript event or function in your application:

从你E序中的js 事g或函数调用之前你已经注册q的相对应函?/p>

< div  id ="someelementid" ></ div >
< button  onclick ="xajax_myfunction(someargument);" >  


that's it. xajax takes care of most everything else. your biggest task is writing the php functions and returning xajax xml responses from them-- which is made extremely easy by the xajaxresponse class.
只需q些步骤。其他的交由xajax d理吧。你最主要的Q务只是编写php中的函数Q只要它们能返回xajax的xml响应p了,而这步可以用xajaxresponse c轻松解冟?/p>


how do i update my content asynchronously?

如何异步更新我的内容?


perhaps the most unique feature of xajax is the xajaxresponse class. other ajax libraries require you to write your own callback handlers in javascript to process the data returned from an asynchronous request and to update the content. xajax, on the other hand, allows you to easily control your content from php. the xajaxresponse class allows you to create xml instructions to return to your application from your php functions. the xml is parsed by xajax message pump and the instructions tell xajax how to update the content and state of your application. the xajaxresponse class currently offers a number of useful commands, such as assign, which sets the specified attribute of an element in your page; append, which appends data to the end of the specified attribute of an element in your page; prepend, which prepends data to the beginning of the specified attribute of an element in your page; replace, which searches for and replaces data in the specified attribute of an element in your page; script, which runs the supplied javascript code; and alert, which shows an alert box with the supplied message text.
xajax最独特的长处也许就?xajaxresponse class了。其它的ajax库需要你亲自写用js写回调的句柄Q来处理一个异步请求而且得到的数据,q更新其内容。另一斚wQxajax只需你简单的控制好php的内宏V然后通过xajaxresponse c,使在你的php函数中创建xml指o(h)q回l你的程序。xml被 xajax的信?pump)解析。其指o(h)告知xajax如何更新内容和你程序中的位|。现在xajaxresponse 已经提供了大量ƈ有帮助的指o(h)Q?a target="_blank">http://www.flaspx.com/weblog/blog.php?bid=16  (?..付上详细的xajaxresponse c说?


a single xml response may contain multiple commands, which will be executed in the order they were added to the response. for example, let's say that a user clicks on a button in your application. the onclick event calls the javascript wrapper for a php function. that wrapper sends an asynchronous request to the server through xmlhttprequest where xajax calls the php function. the php function does a database lookup, some data manipulation, or serialization. you use the xajaxresponse class to generate an xajax xml response containing multiple commands to send back to the xajax message pump to be executed:
一个单独xml响应可以包含多条命o(h)Q他们将依据加入响应的顺序来被执行。D个例子吧Q让我们假设一个用户在你的E序中按下了一个按钮。这个按下的事g调用被js装好的php函数。这个封包通过 xmlhttprequest 发出了一个异步请求给服务器,让xajax调用php函数。这个php函数做了一个查询数据库Q一些数据处理或排序的操作。而你要用 xajaxresponse cL产出一?xajax 的xml响应Q它包含了多条命令。送给xajax 信息pump来执?


$objresponse   =   new  xajaxresponse();

$objresponse -> addassign( " myinput1 " , " value " , $datafromdatabase );
$objresponse -> addassign( " myinput1 " , " style.color " , " red " );
$objresponse -> addappend( " mydiv1 " , " innerhtml " , $datafromdatabase2 );
$objresponse -> addprepend( " mydiv2 " , " innerhtml " , $datafromdatabase3 );
$objresponse -> addreplace( " mydiv3 " , " innerhtml " , " xajax " , " <strong>xajax</strong> " );
$objresponse -> addscript( " var x = prompt(\ " enter your name\ " ); " );

return   $objresponse -> getxml();


the xajax message pump would parse the xml message and perform the following:
xajax信息pump会(x)解析下列xml信息Qƈ执行以下操作:


the value of the element with id myinput1 would be assigned to the data in $datafromdatabase.

变?datafromdatabase赋值给id为myinput1的value元素?/p>


the color of the text in the element with id myinput1 would be changed to red.
id为myinput1的字体颜色元素将被换成红?


the data in $datafromdatabase2 would be appended to the innerhtml of the element with id mydiv1.

$datafromdatabase2,此数据将被追加到id为mydiv1的innerthml元素的结束部?/p>


the data in $datafromdatabase3 would be prepended to the innerhtml of the element with id mydiv2.

$datafromdatabase3,此数据将被添加到id为mydiv2的innerthml元素的开始部?/p>


all occurrences of "xajax" in the innerhtml of the element with id mydiv3 would be replaced with "xajax"; making all of the instances of the word xajax appear bold.
id为mydiv3的innerhtml元素中所有的 "xajax" 被替换?"xajax",使所有的xajax以粗体显C?/p>


a prompt would be displayed asking for the user's name and the value returned from the prompt would be placed into a javascript variable named x.
?x)有一个输入框弹出Qƈ询问用户姓名。从输入框取得的变量{换成js变量q命名ؓ(f)x?br />all of this is implemented on the server side in the php function by forming and returning an xajax xml response.
所有这些组成了php函数在服务器端被执行Q然后传回一个xml响应?/p>

JRobot 2006-09-13 17:38 发表评论
]]>
Xajax中文手册http://www.tkk7.com/JRobot/articles/69422.htmlJRobotJRobotWed, 13 Sep 2006 09:26:00 GMThttp://www.tkk7.com/JRobot/articles/69422.htmlhttp://www.tkk7.com/JRobot/comments/69422.htmlhttp://www.tkk7.com/JRobot/articles/69422.html#Feedback0http://www.tkk7.com/JRobot/comments/commentRss/69422.htmlhttp://www.tkk7.com/JRobot/services/trackbacks/69422.html

Xajax中文手册(W一?

日期Q?/strong>2006-02-13 17:02:00  点击Q?/strong>2678  作者:(x)  来源Q?/strong>

原文Qhttp://xajax.sourceforge.net/
英文原版QCopyright © 2005 J. Max Wilson
体中文翻译:(x)HonestQiao(乔楚)/2005-12-7 17:23/(W一?
  • 什么是xajax?
  • xajax如何工作?
  • Z么我要用xajax代替其他PHP的Ajax?
  • 如何在我的PHP脚本之中使用xajax?
  • 如何异步更新内容?
  • 如何异步处理表单数据?
  • 如何lxajax增加定制功能?
  • 我能在私有或者收费品之中用xajax?
 
  • What is xajax?
  • How does xajax work?
  • Why should I use xajax instead of another Ajax library for PHP?
  • How do I use xajax in my PHP script?
  • How do I update my content asynchronously?
  • How do I process form data asynchronously?
  • How do I add custom functionality to xajax?
  • May I use xajax in a proprietary product and charge for it?
什么是xajax?
Xajax是一个开源的 PHP cd 它能够让你黏合HTML、CSS、JavaScript和PHPQ轻而易丄开发功能强大、基于WEB的AJAX应用软g. 使用xajax开发的应用软gQ无需重新调入面Q就能够异步调用服务器端的PHP函数和更新内?
What is xajax?
xajax is an open source PHP class library that allows you to easily create powerful, web-based, Ajax applications using HTML, CSS, JavaScript, and PHP. Applications developed with xajax can asynchronously call server-side PHP functions and update content without reloading the page.
 
xajax 如何工作?
你的应用软g需要异步调用的PHP函数Q?xajax的PHP对象都生成了对应的封装好了的JavaScript函数. 当被调用Ӟ装的函C用JavaScript的XMLHttpRequest对象与服务器异步通讯Q调用xajax对象对应的PHP函数. 调用l束? PHP函数由xajaxq回一个xajax的XML响应传递给应用E序. XML响应包含了特定的指o(h)和数据,他们可以被xajax的JavaScript消息分析器解析,q且被用于更C的应用程序的内容.
How does xajax work?
The xajax PHP object generates JavaScript wrapper functions for the PHP functions you want to be able to call asynchronously from your application. When called, these wrapper functions use JavaScript's XMLHttpRequest object to asynchronously communicate with the xajax object on the server which calls the corresponding PHP functions. Upon completion, an xajax XML response is returned from the PHP functions, which xajax passes back to the application. The XML response contains instructions and data that are parsed by xajax's JavaScript message pump and used to update the content of your application.
Z么我要用xajax代替其他PHP的ajax?
你应该选择一个最是和你的目需要的?
xajax 提供了以下的功能, 它们使得ajax富有特色而又功能强大:
Why should I use xajax instead of another Ajax library for PHP?
You should choose whatever library will best meet the needs of your project.
xajax offers the following features that, together, make it unique and powerful:
  • XajaxҎ(gu)?b> XML 响应 / javascript 消息分析pȝ 帮助你做? 自动的处理函数返回的数据Q按照PHP函数q回的指令更新内Ҏ(gu)者状? 因ؓ(f)xajax作了q些工作Because xajax does the work, 你不需要写javascript的回调处理函?
  • xajax's unique XML response / javascript message-pump system does the work for you, automatically handling the data returned from your functions and updating your content or state according to the instructions you return from your PHP functions. Because xajax does the work, you don't have to write javascript callback handler functions.
  • Xajax反对代码和数据紧密地杂p在一L(fng)d, q且保持xajax的代码从与他代码分离. 因ؓ(f)它是对象构造的, 你可以加上自己定制的功能lxajaxL展xajaxResponse cd使用addScriptҎ(gu).
  • xajax is object oriented to maintain tighter relationships between the code and data, and to keep the xajax code separate from other code. Because it is object oriented, you can add your own custom functionality to xajax by extending the xajaxResponse class and using the addScript() method.
  • xajax 可以工作?Firefox, Mozilla, 大部分基?Mozilla 的浏览器, Internet Explorer, ?Safari.
  • xajax works in Firefox, Mozilla, probably other Mozilla based browsers, Internet Explorer, and Safari.
  • 除了更新元素的值和内含的HTML内容(innerHTML), xajax q能用于更新样式(styles), css c? 多选和单选按钮选择,甚至可以更新M元素的属?
  • In addition to updating element values and innerHTML, xajax can be used to update styles, css classes, checkbox and radio button selection, or nearly any other element attribute.
  • xajax 支持使用一l或者多l数l、关联数l?哈希数组) 作ؓ(f)xajax函数的参Cjavascript传送给PHP. 反之Additionally, 如果你传送一个javascript的对象给xajax函数QPHP函数接受一?b>描叙对象属?/b>的关联数l?哈希数组).
  • xajax supports passing single and multidimensional arrays and associative arrays from javascript to PHP as parameters to your xajax functions. Additionally, if you pass a javascript object into an xajax function, the PHP function will receive an associative array representing the properties of the object.
  • xajax 提供了一U?b>单的异步表单处理方式. 使用 xajax.getFormValues() javascript Ҏ(gu), 你可以轻而易丄在表单之中提交一个描l值的数组作ؓ(f)参数传送到xajax异步处理函数:
xajax_processForm(xajax.getFormValues('formId');
. 它可以处理复杂的input 元素名称 Q例?"checkbox[][]" 或?"name[first]" 产生的多l或者关联数l?哈希数组), 是普通提交表单那样用PHP?_GET数组
  • xajax provides easy asynchronous Form processing. By using the xajax.getFormValues() javascript method, you can easily submit an array representing the values in a form as a parameter to a xajax asynchronous function:
xajax_processForm(xajax.getFormValues('formId');
. It even works with complex input names like "checkbox[][]" and "name[first]" to produce multidimensional and associative arrays, just as if you had submitted the form and used the PHP $_GET array
  • 使用xajax你可以动态的发送附加的javascript作ؓ(f)h的响应到你的应用软g中运行,而这和动态的更新元素的属性一h?
  • Using xajax you can dynamically send additional javascript to your application to be run in response to a request as well as dynamically update element attributes.
  • Xajax?x)自动比较PHP函数q回的数据与你已l标记需要修改的元素属? 只有当新的数据确实可以改变现有的属性,属性才?x)真的被更? q将可消除程序在一定时间间隔内更新与当前内容相同或者不同的内容而出现的明显的闪?
  • xajax automatically compares the data returned from the PHP functions to the data that is already in the attribute of the element you have marked for change. The attribute is only updated with the new data if it will actually change what is already in the attribute. This eliminates the flicker often observed in applications that update content at a regular time interval with data which may or may not differ from extant content.
  • 每一个通过xajax单注册的函数都可以有不同的请求方? 所有的函数默认使用POST方式Q除非你明确的指定用GET方式. 如果使用GEThQ你必须非常明确的考虑它确实是你所需要的
  • Each function registered to be accessible through xajax can have a different request type. All functions default to use POST unless GET is explicitly set. This is to encourage careful consideration of when to use GET requests
  • 如果没有指定URIQxajax设法自动确定脚本的URI. Xajax的自动确定算法经q了充分的验证,保在大部分HTTPS或者未知端口的HTTP服务器上也可以正常的工作.
  • If no request URI is specified, xajax tries to autodetect the URL of the script. The xajax autodetection algorithm is sophisticated enough that, on most servers, it will work under a secure https:// protocol as well as http:// and with nonstandard ports.
  • Xajax使用UTF8~码所有的h和响应,以确保支持绝大部分的字符和语a. Xajax已经成功试了多U不同的UNICODE字符Q包?b>Spanish, Russian, Arabic, and Hebrew
  • xajax encodes all of its requests and responses in UTF-8 so that it can support a wider range of characters and languages. xajax has been successfully tested with various unicode characters including Spanish, Russian, Arabic, and Hebrew
  • 几乎所有xajax生成的javascript都可以通过动态的外部javascript包含C的webE序之中. 当你在浏览器之中查看E序的源代码Ӟ不会(x)有JavaScript的函数定义؜杂在HTML标记之中.
  • Nearly all of the javascript generated by xajax is included into your web application through dynamic external javascript. When you view the source of your application in your browser, the markup will be not cluttered by JavaScript function definitions.
  • Xajax可以使用在Smarty模板pȝ之中Q仅仅需要创Z个smarty变量可以包含xajax的javascript:
$smarty->assign('xajax_javascript', $xajax->getJavascript());
然后你可以用在header模版之中使用
{$xajax_javascript}
从而把xajax应用C的站?
  • xajax can be used with the Smarty templating system by creating a variable in smarty that contains the xajax javascript:
$smarty->assign('xajax_javascript', $xajax->getJavascript());
Then you can use
{$xajax_javascript}
in your header template to use xajax on your site.
如何在我的PHP脚本之中使用xajax?
Xajax的设计是如此的富有特Ԍ以至于不是已有的webE序q是新的目Q它都能够被极其单的部v和应? 仅仅需要七步,你就可以在几乎Q何PHP脚本之中加入xajax的强大功?
How do I use xajax in my PHP script?
xajax is designed to be extremely easy to implement in both existing web applications as well as new projects. You can add the power of xajax to nearly any PHP script in seven easy steps:
  1. 包含xajaxcd:
require_once("xajax.inc.php");
  1. 实例化xajax 对象:
$xajax = new xajax();
  1. 注册你需要通过xajax调用的PHP函数的名U?
$xajax->registerFunction("myFunction");
  1. ~写注册的PHP函数Qƈ且在函数之中使用xajaxResponse 对象q回XML指o(h):
function myFunction($arg)
{
   // ?arg做一些基本处理例如从数据库检索数?/div>
   // 然后把结果赋值给变量Q例?newContent
  
   // 实例化xajaxResponse 对象
   $objResponse = new xajaxResponse();
  
   // d指o(h)到响应之中,用于指派
   //指定元素(例如id="SomeElementId")的innerHTML属性的新的内容
   $objResponse->addAssign("SomeElementId","innerHTML", $newContent);
  
   //q回xajaxResponse 对象生成的XML响应
   return $objResponse->getXML();
}
  1. 在你的脚本输ZQ何信息之前,调用xajax用于接管h:
$xajax->processRequests();
  1. 在页面的 <head></head> 标签之间, 告诉xajax生成所必需的JavaScript:
<?php $xajax->printJavascript(); ?>
  1. 在程序中Q从JavaScript事g或者函数调用前面注册的函数:
<div id="SomeElementId"></div>
<button onclick="xajax_myFunction(SomeArgument);">
  1. Include the xajax class library:
require_once("xajax.inc.php");
  1. Instantiate the xajax object:
$xajax = new xajax();
  1. Register the names of the PHP functions you want to be able to call through xajax:
$xajax->registerFunction("myFunction");
  1. Write the PHP functions you have registered and use the xajaxResponse object to return XML commands from them:
  2. function myFunction($arg)
  3. {
  4.   // do some stuff based on $arg like query data from a database and
  5.   // put it into a variable like $newContent
  6.   // Instantiate the xajaxResponse object
  7.   $objResponse = new xajaxResponse();
  8.   // add a command to the response to assign the innerHTML attribute of
  9.   // the element with id="SomeElementId" to whatever the new content is
  10.   $objResponse->addAssign("SomeElementId","innerHTML", $newContent);
  11.   //return the XML response generated by the xajaxResponse object
  12.   return $objResponse->getXML();
}
  1. Before your script sends any output, have xajax handle any requests:
$xajax->processRequests();
  1. Between your <head></head> tags, tell xajax to generate the necessary JavaScript:
<?php $xajax->printJavascript(); ?>
  1. Call the function from a JavaScript event or function in your application:
  2. <div id="SomeElementId"></div>
<button onclick="xajax_myFunction(SomeArgument);">
 
p么简? xajax ?x)处理其他所有的事情. 你所要做的主要工作就是编写PHP函数Q然后从函数之中q回xajax的XML响应。而后者通过xajaxResponsecd以非常简单的生成.
That's it. xajax takes care of most everything else. Your biggest task is writing the PHP functions and returning xajax XML responses from them-- which is made extremely easy by the xajaxResponse class.
 
如何异步更新内容?
Xajax最富有特色的功能或许就是xajaxResponsec? 其他的Ajax库需要你自己~写JavaScript的回调句柄去处理一个异步请求返回的数据q更新内? xajax, 从另外一个角度来? 允许你用PHP单的控制内容. xajaxResponse 让你在PHP函数之中创徏XML指o(h)q回l你的程? XML被xajax的消息分析器解析Q指令将告诉xajax如何更新E序的内容和状? xajaxResponsecȝ前提供了以下指o(h):
How do I update my content asynchronously?
Perhaps the most unique feature of xajax is the xajaxResponse class. Other Ajax libraries require you to write your own callback handlers in JavaScript to process the data returned from an asynchronous request and to update the content. xajax, on the other hand, allows you to easily control your content from PHP. The xajaxResponse class allows you to create XML instructions to return to your application from your PHP functions. The XML is parsed by xajax message pump and the instructions tell xajax how to update the content and state of your application. The xajaxResponse class currently offers the following commands:
  • addAssign($sTargetId,$sAttribute,$sData)
    l命名ؓ(f)$sTargetId的元素的$sAttribute属性赋?sData
$objResponse->addAssign("contentDiv","innerHTML","Some Text");
$objResponse->addAssign("checkBox1","checked","true");
  • addAssign($sTargetId,$sAttribute,$sData)
    Assigns the $sAttribute of the element identified by $sTargetId to $sData
$objResponse->addAssign("contentDiv","innerHTML","Some Text");
$objResponse->addAssign("checkBox1","checked","true");
  • addAppend($sTargetId,$sAttribute,$sData)
    l命名ؓ(f)$sTargetId的元素的$sAttribute属性追加?sData
$objResponse->addAppend("contentDiv","innerHTML","Some Text");
  • addAppend($sTargetId,$sAttribute,$sData)
    Appends $sData to the $sAttribute of the element identified by $sTargetId
$objResponse->addAppend("contentDiv","innerHTML","Some Text");
  • addPrepend($sTargetId,$sAttribute,$sData)
    预备l命名ؓ(f)$sTargetId的元素的$sAttribute属性赋?sData
$objResponse->addPrepend("contentDiv","innerHTML","Some Text");
  • addPrepend($sTargetId,$sAttribute,$sData)
    Prepends $sData to the $sAttribute of the element identified by $sTargetId
$objResponse->addPrepend("contentDiv","innerHTML","Some Text");
  • addReplace($sTargetId,$sAttribute,$sSearch,$sData)
    替换命名?sTargetId的元素的$sAttribute属性的g中的$sSearch?sData
$objResponse->addReplace("contentDiv","innerHTML","text","<strong>text</strong>");
  • addReplace($sTargetId,$sAttribute,$sSearch,$sData)
    replaces all instances of $sSearch with $sData in the $sAttribute of the element identified by $sTargetId
$objResponse->addReplace("contentDiv","innerHTML","text","<strong>text</strong>");
  • addClear($sTargetId,$sAttribute)
    清空命名?sTargetId的元素的$sAttribute属性的?
$objResponse->addClear("Input1","value");
  • addClear($sTargetId,$sAttribute)
    Clears the $sAttribute of the element identified by $sTargetId
$objResponse->addClear("Input1","value");
  • addCreate($sParentId, $sTagname, $sId, $sType)
    在已l存在的命名?sParentId的元素下d一个名?sTagName的子元素Qƈ且把他的id赋gؓ(f)$sIdQ可选的cd赋gؓ(f) $sType.
$objResponse->addCreate("form1","input", "pass", "password");
  • addCreate($sParentId, $sTagname, $sId, $sType)
    Adds a new $sTagName child element to an existing element identified by $sParentId, and assigns it the id $sId and the optional type $sType.
$objResponse->addCreate("form1","input", "pass", "password");
  • addRemove($sElementId)
    从你的程序之中移除命名ؓ(f)$sElementId的元?/li>
$objResponse->addRemove("div1");
  • addRemove($sElementId)
    Removes the element identified by $sElementId from your application
$objResponse->addRemove("div1");
  • addAlert($sMsg)
    昄一个内容ؓ(f) $sMsg 的警告框(JavaScript的Alert)
$objResponse->addAlert("This is some text");
  • addAlert($sMsg)
    Display an alert box with $sMsg
$objResponse->addAlert("This is some text");
  • addScript($sJS)
    执行JavaScript代码 $sJS (演示代码原文有误)
$objResponse->addScript("var txt = prompt('get some text');");
  • addScript($sJS)
    Execute the JavaScript code $sJS
$objResponse->addAlert("var txt = prompt('get some text');");
 
一个独立的XML响应可能包含多个指o(h), 他们按照加入响应的序执行. 让我们用一个用户在你的E序之中点击按钮Z来进行说? Onclick事g调用PHP函数对应的javascript装.q个装通过XMLHttpRequest发送异步请求到服务器给xajax调用PHP函数. PHP函数做了一ơ数据库查询, 处理了一些数? 或者序列化. 然后你用xajaxResponsecȝ成包含多个指令的xajax的XML响应 Qƈ回送给xajax的消息分析器执行:
A single XML response may contain multiple commands, which will executed in the order they were added to the response. For example, let's say that a user clicks on a button in your application. The onclick event calls the javascript wrapper for a PHP function. That wrapper sends an asynchronous request to the server through XMLHttpRequest where xajax calls the PHP function. The PHP function does a database lookup, some data manipulation, or serialization. You use the xajaxResponse class to generate an xajax XML response containing multiple commands to send back to the xajax message pump to be executed:
    $objResponse = new xajaxResponse();
    $objResponse.addAssign("myInput1","value",$DataFromDatabase);
    $objResponse.addAssign("myInput1","style.color","red");
    $objResponse.addAppend("myDiv1","innerHTML",$DataFromDatabase2);
    $objResponse.addPrepend("myDiv2","innerHTML",$DataFromDatabase3);
    $objResponse.addReplace("myDiv3","innerHTML","xajax","<strong>xajax</strong>");
    $objResponse.addScript("var x = prompt("Enter Your Name");");
           
    return $objResponse->getXML();
 
$objResponse = new xajaxResponse();
    $objResponse.addAssign("myInput1","value",$DataFromDatabase);
$objResponse.addAssign("myInput1","style.color","red");
$objResponse.addAppend("myDiv1","innerHTML",$DataFromDatabase2);
$objResponse.addPrepend("myDiv2","innerHTML",$DataFromDatabase3);
$objResponse.addReplace("myDiv3","innerHTML","xajax","<strong>xajax</strong>");
$objResponse.addScript("var x = prompt("Enter Your Name");");
return $objResponse->getXML();
xajax消息分析器将?x)解析XML消息Qƈ执行以下工作:
  1. id为myInput1的元素的值将被赋gؓ(f) $DataFromDatabase的数?
  2. id为myInput1的元素的颜色会(x)变ؓ(f)red.
  3. $DataFromDatabase2的数据会(x)被追加到id为myDiv1的元素innerHTML之中.
  4. $DataFromDatabase3的数据会(x)被预先赋值给id为myDiv2的元素innerHTML之中.
  5. id为myDiv3的元素的innerHTML 之中所有的"xajax"被替换?<strong>xajax</strong>"; 使得所有的单词 xajax 昄加粗.
  6. 一个提C框会(x)昄Q用来询问用户姓名,从提C框q回的g(x)被命名ؓ(f)x的javascript变量接收.
The xajax message pump would parse the XML message and perform the following:
  1. The value of the element with id myInput1 would be assigned to the data in $DataFromDatabase.
  2. The color of the text in the element with id myInput1 would be changed to red.
  3. The data in $DataFromDatabase2 would be appended to the innerHTML of the element with id myDiv1.
  4. The data in $DataFromDatabase3 would be prepended to the innerHTML of the element with id myDiv2.
  5. All occurrences of "xajax" in the innerHTML of the element with id myDiv3 would be replaced with "<strong>xajax</strong>"; making all of the instances of the word xajax appear bold.
  6. a prompt would be displayed asking for the user's name and the value returned from the prompt would be placed into a javascript variable named x.
所有这些都由构成的PHP函数在服务器端执行ƈq回xajax的XML响应.
All of this is implemented on the server side in the PHP function by forming and returning an xajax XML response.
如何异步处理表单数据?
Xajax使得异步处理表单数句非常非常的简? xajax.getFormValues()Ҏ(gu)?x)自动的从表单提取数据,q作Z个参数提交给xajax注册的PHP函数.
xajax.getFormValues() 仅仅需要一个参? 可以是你需要处理得表单的id, 或者是一个实际的表单对象. 你也可以使用xajax.getFormValues作ؓ(f)一个参数给xajax 函数, 例如:
xajax_processFormData(xajax.getFormValues('formId'));
xajax ?x)生成一个与表单数据对应的请求字W串lxajax服务器解析,然后以一个与表单数据对应的数l传递给PHP函数Q就想你提交表单使用PHP?_GET数组那么?
Xajax可以处理cM普通多l数l或者联合数l?哈希数组){Ş式的复杂输入名字. 例如, 如果一个表单有三个多选框(checkboxes)q且都命名ؓ(f) "checkbox[]", 但是值分别ؓ(f) "check1", "check2", ?"check3", 然后使用 xajax.getFormValues 函数作ؓ(f)参数传递给xajax 函数, ?PHP 函数?x)接受到一个如下的数组:
array (
  'checkbox' =>
  array (
    0 => 'check1',
    1 => 'check2',
    2 => 'check3',
  ),
)
作ؓ(f)函数参数的数l的l构与传l意义上提交表单之后?_GET数组的结构相? 你可以访问数l之中的checkbox 的数? $aFormData['checkbox'][0]
How do I process form data asynchronously?
xajax makes processing form data asynchronously extremely easy. The xajax.getFormValues() method can be used to automatically extract the data from a form and pass it as a parameter to a PHP function you have registered with xajax.
xajax.getFormValues() takes one argument, which can be either the id of the form you want to process, or the actual form object. You use xajax.getFormValues as a parameter to your xajax function, like this:
xajax_processFormData(xajax.getFormValues('formId'));
xajax generates a query string representing the form data which is parsed by the xajax server and passed to your PHP function as an array representing the form data, just as if you had submitted the form and used the PHP $_GET array.
xajax will even handle complex input names to generate multidimensional and associative arrays. For instance, if you have a form with three checkboxes and you give them all the name "checkbox[]", but different values like "check1", "check2", and "check3", and you use the xajax.getFormValues function as a parameter to your xajax function, the PHP function will receive and array that looks like this:
array (
  'checkbox' =>
  array (
    0 => 'check1',
    1 => 'check2',
    2 => 'check3',
  ),
)
The array argument to your function mirrors the structure that the $_GET array would have if you were to submit the form traditionally. You can then access the checkbox data in the array like this:
$aFormData['checkbox'][0]
如何lxajax增加定制功能?
Xajax可以使用各种服加的用户定制功能进行扩? 正因为xajax是完全面向对象的Qƈ且可以用xajaxResponse的addScript()Ҏ(gu)Q所以他h无限扩展的可? 你可以创Z自己的xajax响应c,来承xajaxResponse cM?qing)它的方法,q加上你自己定制的响? 让我们用一个定制的增加选择l合?select combo boxes)选项的响应指令的例子来说? 你可以象下面q样扩展xajaxResponse c?
class myXajaxResponse extends xajaxResponse
  function addAddOption($sSelectId, $sOptionText, $sOptionValue) 
  { 
    $sScript  = "var objOption = new Option('".$sOptionText."','".$sOptionValue."');";
    $sScript .= "document.getElementById('".$sSelectId."').options.add(objOption);";
    $this->addScript($sScript);
  }
}
现在, 取代xajaxResponse 对象的初始化, 把你自己?myXajaxResponse 对象的初始化定义C?xajax PHP 函数之中:
$objResponse = new myXajaxResponse();
$objResponse->addAssign("div1", "innerHTML", "Some Text"); 
$objResponse->addAddOption("select1","New Option","13"); 
 
return $objResponse->getXML();
被调用时Q这个方法将?x)发送需要的javascript到页面ƈ执行. 当然你也有另外一U做法Alternatively, 你可以在你的E序之中创徏一个如下的javascript函数:
<script type="text/javascript">
function addOption(selectId,txt,val)
{
        var objOption = new Option(txt,val);
        document.getElementById(selectId).options.add(objOption);
}
</script>
q且使用addScript() 调用q个Ҏ(gu):
$objResponse->addScript("addOption('select1','New Option','13');");
How do I add custom functionality to xajax?
xajax can be extended with all kinds of additional custom functionality. The extendability of xajax is made possible because it is object oriented, and by the addScript() method of the xajaxResponse class. You can create your own xajax response class that extends the xajaxResponse class and has all of the normal xajaxResponse methods, plus your own custom responses. For instance, let's say that you wanted to add a custom response command to add options to select combo boxes. You could extend the xajaxResponse class like this:
class myXajaxResponse extends xajaxResponse
  function addAddOption($sSelectId, $sOptionText, $sOptionValue) 
  {  
    $sScript  = "var objOption = new Option('".$sOptionText."','".$sOptionValue."');";
    $sScript .= "document.getElementById('".$sSelectId."').options.add(objOption);";
    $this->addScript($sScript);
  }
}
Now, instead of instantiating an xajaxResponse object, you instantiate and use your myXajaxResponse object in your xajax PHP functions:
$objResponse = new myXajaxResponse();
$objResponse->addAssign("div1", "innerHTML", "Some Text"); 
$objResponse->addAddOption("select1","New Option","13"); 
 
return $objResponse->getXML();
This method would send the necessary javascript to the page when it was called and execute it. Alternatively, you could create a javascript function in your application:
<script type="text/javascript">
function addOption(selectId,txt,val)
{
        var objOption = new Option(txt,val);
        document.getElementById(selectId).options.add(objOption);
}
</script>
and call it with the addScript() method:
$objResponse->addScript("addOption('select1','New Option','13');");
我能在私有或者收费品之中用xajax?
而言? 能,只要你愿?
xajax PHP cd的发布遵?GNU Lesser General Public License (LGPL).
May I use xajax in a proprietary product and charge for it?
In short: Yes, you may.
The xajax PHP class library is released under the GNU Lesser General Public License (LGPL).
 
体中文手册版权所?© 2005 HonestQiao(乔楚)


JRobot 2006-09-13 17:26 发表评论
]]>用Google Ajaxslt 生成首页新闻http://www.tkk7.com/JRobot/articles/69405.htmlJRobotJRobotWed, 13 Sep 2006 08:57:00 GMThttp://www.tkk7.com/JRobot/articles/69405.htmlhttp://www.tkk7.com/JRobot/comments/69405.htmlhttp://www.tkk7.com/JRobot/articles/69405.html#Feedback0http://www.tkk7.com/JRobot/comments/commentRss/69405.htmlhttp://www.tkk7.com/JRobot/services/trackbacks/69405.html
XMLHTTPRequest 从上ơ做q一个手机模拟器后就再没过Q这ơ在|上搜寻发现了一个好东西Q?a target="_blank">http://www.scss.com.au/family/andrew/webdesign/xmlhttprequest/Q一?Cross-Browser ?XMLHttpRequest 实现Q作者在创作q条款QCreative Commons LicenseQ下发布q个库,只要你不U除脚本中作者的姓名和网址可以自׃用,q它了Q下?xmlhttprequest.js 以备后用?br />
数据可以保存在数据库中也可以单的写入到服务器的一个文件中Qؓ(f)了灵zv见我在 MySQL 中新Z个表攄内新MQ就q么几个字段Qid、title、content、time。新d入和从数据库中取数据提供l?Ajax/Ajah h略?br />
下面是客L(fng) XMLHTTPRequest 的代码:(x)

<html><head><title>首页新闻试</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><script type="text/javascript" src="xmlhttprequest.js"></script></head><body><div id="News">新闻d中…?lt;/div><script type="text/javascript">var req = new XMLHttpRequest();if( req ){req.onreadystatechange = function(){var News = "新闻dp|";try{if( req.readyState == 4 && ( req.status == 200 || req.status == 304 ) )News = req.responseText;}catch(e){News = e.description;}document.getElementById( 'News' ).innerHTML = News;};req.open( 'GET', 'news.php' );req.send( null );}</script></body></html>



q里唯一值得说明的就?XMLHTTPRequest 默认是用 UTF-8 传递数据的Q所以你的数据源最好也?UTF-8 否则Q因为我们这里客L(fng)已经指定了要?GB2312 了)得到的会(x)是ؕ码,q在 IE 下会(x)引发 JavaScript 错误Q在 FireFox 下却没问题,所以我?try ?catch 把上面的代码包住了,免得试的时候出现错误。如果想试试的话可以用下面的代码作ؓ(f) news.phpQ?br />
<?phpecho iconv( "GB2312", "UTF-8", "q就是新? );//echo "q就是新?;?>



如果注释掉第一句而开攄二句的话不仅看到的是qQ在 IE 下还?x)出?-1072896748 错误。有意思的?IE 好像q会(x)~存?XMLHTTPRequest 收到的内容,?FireFox 则不?x),所以在试中会(x)出现h IE 很多ơ看到的q是旧数据的问题Q可以在E序里加上下?4 句话解决q个问题Q?

header( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );header( "Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT" );header( "Cache-Control: no-cache, must-revalidate" );header( "Pragma: no-cache" );



要将首页新闻提供l聚合器订阅p后台输入的新闻生成 RSS feedQRSS 2.0 的规范可以参?http://feedvalidator.org/docs/rss2.htmlQ相应的中文译版本可以?http://www.cpcwedu.com/Document/WEBOfficial/095447158.htm 扑ֈ?br />
RSS 格式其实也是 XML 众多方言中的一U,所以也要以 开_(d)根节点的名称必须?rssQ符?RSS 2.0 规范?RSS 中这?rss 节点要有一?version="2.0" 的属性。rss 节点有一个名?channel 的子节点Qchannel 是一个频道。channel ?3 个必要的子节点,分别?title、link ?description。以 Blog ZQtitle 是 Blog 的名字,link ?Blog ?URL 地址Q?description ?Blog 的描q?br />


channel 可以包含若干?item 子节点,?Blog 中每一?item 对应一?postQ我们这里每一?item 是一新闅Ritem 的所有子节点均ؓ(f)可选,但至要包含 title ?descriptionQ由于新闻要有时_(d)所以我们再l它加上一?pubDate 节点Q下面就是一个符?RSS 2.0 规范?RSS 文g内容Q?br />
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title>The name of my site</title><link>http://www.mysite.com</link><description>Just for testing</description><item><title>About</title><description>Hi, I'm 2ndboy. Welcome to my site!</description><pubDate>Sat, 07 Sep 2005 0:00:01 GMT</pubDate></item><item><title>New service is out!</title><description>It's great for using.</description><pubDate>Sat, 21 Sep 2005 1:23:45 GMT</pubDate></item></channel></rss>


从数据库中读取最新的新闻内容生成 RSS 的代码这里就不脓(chung)了,无非是一些字W串的拼接,当然了,你也可以?PHP ?XML DOM 接口来生成这?RSS 内容。这样一来在|站上提?RSS 订阅搞定了?br />
在做?RSS 部分后我H然有了一个想法,Zl?Ajax 取数据的览器提供信息,服务器要针对 Ajax 的请求单独作些响应处理,那何不就让浏览器把我们前面已l生成了?RSS 当作数据源来昄首页新闻呢?q似乎是个不错的xQ但 RSS 是个 XML 格式的文Ӟ很显Ӟ惌到这L(fng)目的p让客L(fng)?JavaScript 来解?XML 文gq加以显C?br />
Z?JavaScript 来操?XMLQ我军_使用 Google 的一个开源项目——AjaXSLTQ?a target="_blank">http://goog-ajaxslt.sourceforge.net/Q,更多 Google 开源项目可以访?http://code.google.com/。Google 提供的这?AjaXSLT 目?JS 实现了一?XML ?DOM 接口和一?XSLT 实现Q下面把里面的几个主要文件做个简单介l:(x)

misc.jsQ一些常量定义和 helper 函数Q还?log 的实现。dom.jsQXML DOM 接口?JS 实现Q主要的函数?xmlParse( XMLString )Q调用成功后可以用标准 DOM 方式来操控返回的 XDocument 了?br />
xpath.jsQXPath ?JS 实现?br />
xslt.jsQXSLT ?JS 实现Q要用到里面?xsltProcess( XML, XSLT )Q给?XML ?XSLT 得到转换的结果?br />
你可以只使用其中?DOM 实现部分Q?dom.js 又依赖于 misc.jsQ所以要先蝲?misc.js 再蝲?dom.js。ؓ(f)了在首页上显C好看的新闻Q我们要?CSS 来打扮一下新ȝ外观Q所以要Ҏ(gu)ȝ数据l织方式有些要求Q比如说要把得到的新Ll组l成下面q个样子

<div id="News"><div class="News"><span class="Title">title</span><span class="Time">time</span><span class="Content">content</span></div></div>

q样之后我们可以事先写一些针?#News ?div.News {等的规则来装扮首页新闻了。好了,下面看看?JS 解析 RSS 的代码怎么写(当然Q之前我们先要用 Ajax 取到 RSS 数据Q:(x) var News = "";var doc = xmlParse( rss );items = doc.getElementsByTagName( "item" );for( var i = 0; i < items.length; i++ ){News += "<div class='News'>";title = items[i].getElementsByTagName( "title" );News += ( "<span class='Title'>" + title[0].firstChild.nodeValue + "<\/span>" );time = items[i].getElementsByTagName( "pubDate" );News += ( "<span class='Time'>" + time[0].firstChild.nodeValue + "<\/span>" );description = items[i].getElementsByTagName( "description" );News += ( "<span class='Content'>" + description[0].firstChild.nodeValue + "<\/span>" );News += "<\/div>";}document.getElementById('News').innerHTML = News;


好了Q到现在为止我们的首|d完工了Q但是既?AjaXSLT 提供?XSLT ?JS 实现Q我们拿来试试。先照着《XML 高~程》写?XSLTQ?br />
<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><xsl:apply-templates select="http://item"/></xsl:template><xsl:template match="item"><div class="News"><xsl:apply-templates select="title"/><xsl:apply-templates select="pubDate"/><xsl:apply-templates select="description"/></div></xsl:template><xsl:template match="title"><span class="Title"><xsl:value-of select="."/></span></xsl:template><xsl:template match="pubDate"><span class="Time"><xsl:value-of select="."/></span></xsl:template><xsl:template match="description"><span class="Content"><xsl:value-of select="."/></span></xsl:template></xsl:stylesheet>



我们上面?JavaScript 代码q要把这?XSLT 从服务器上取下来Q之后一句话可以搞定上面一?JS 代码才能完成的工作:(x)document.getElementById('News').innerHTML = xsltProcess( xmlParse( rss ), xmlParse( xslt ) );

OKQ大功告成!注意Q由?XSLT 在{换时使用?XPathQ所以要先包?xpath.js 再引?xslt.js?br />

JRobot 2006-09-13 16:57 发表评论
]]>
Google Ajaxslt 使用http://www.tkk7.com/JRobot/articles/69403.htmlJRobotJRobotWed, 13 Sep 2006 08:50:00 GMThttp://www.tkk7.com/JRobot/articles/69403.htmlhttp://www.tkk7.com/JRobot/comments/69403.htmlhttp://www.tkk7.com/JRobot/articles/69403.html#Feedback0http://www.tkk7.com/JRobot/comments/commentRss/69403.htmlhttp://www.tkk7.com/JRobot/services/trackbacks/69403.html先去下蝲google ajaxslt的源码包

1、用xmlhttprequest从服务器获得xml数据放于id="xml"的div?/p>

也可以从各种装的xmlhttprequest框架中直接获得xmlQ这U方式更好一?/p>

2、对应xsl攄于id="xslt"的div?/p>

3、通过Ajaxsltl合成htmlQ结果将昄于id="htmldisplay"的div?/p>

加蝲如下js

  <script src="../misc.js" type="text/javascript"></script>  <script src="../dom.js" type="text/javascript"></script>  <script src="../xpath.js" type="text/javascript"></script>  <script src="../xslt.js" type="text/javascript"></script>

然后

  var xml = xmlParse(el('xml').value);  var xslt = xmlParse(el('xslt').value);  var html = xsltProcess(xml, xslt);  document.getElementById('html').value = html;  document.getElementById('htmldisplay').innerHTML = html;

很简单就完成了xml到html的{化,而xpath/xsl的强大功能得我们可以非常更斚w的处理xml数据

备注:

misc.jsQ一些常量定义和 helper 函数Q还?log 的实现?/p>

dom.jsQXML DOM 接口?JS 实现Q主要的函数?xmlParse( XMLString )Q调用成功后可以用标准 DOM 方式来操控返回的 XDocument 了?/p>

xpath.jsQXPath ?JS 实现?/p>

xslt.jsQXSLT ?JS 实现Q要用到里面?xsltProcess( XML, XSLT )Q给?XML ?XSLT 得到转换的结果?/p>

JRobot 2006-09-13 16:50 发表评论
]]>
վ֩ģ壺 3dѶ߹ۿ| Ʒһ߹ۿ| ޵Ӱһ| ޹ƷɫƬۿ | Ʒ޾߹ۿ | 99þùĻ| þùƷվ| 99ֻоƷ6| йxxxxxѿƵ| 18paoƵ| 2020þþƷ| Ƶ߹ۿַ| 99ƵѹۿƵ| ŮſȵͰƵ | þþƷѿ| AV߲| һëƬѲ| һvһ| 뾫ƷAVӰ| Ƶ97| A뾫ƷAA | ԻƵ40Կ| ѸƵ| ѧһëƬ| պ޹ۺϾþþ| ҹƵ| ޾Ʒmv߹ۿ| þúݺݸ߳޾Ʒ| þ޾Ʒϵַ| avһ߲| Ƶ߹ۿ| ŮƵƵƵҳ | ߿Ƭ˳Ƶ | ޵һseվ| ǵӰȫ| ĻmvƵ| һһһƵѿ| Ů˱˾޴| ޹| ҹƵ| һѵӰ|