PHP-Ajax XML分析器
PHP-Ajax XML解析器 Ajax XML示例 与Ajax一起使用,我们可以解析本地目录和服务器中的xm…
PHP-Ajax XML解析器
Ajax XML示例
与Ajax一起使用,我们可以解析本地目录和服务器中的xml,下面的示例演示如何使用Web浏览器解析xml。
<script>
function showCD(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","getcourse.php?q="+str,true);
xmlhttp.send();
}
</script>
<form>
Select a Course:
<select name="cds" onchange="showCD(this.value)">
<option value="">Select a course:</option>
<option value="Android">Android </option>
<option value="Html">HTML</option>
<option value="Java">Java</option>
<option value="Microsoft">MS technologies</option>
</select>
</form>
<div id="txtHint"><b>Course info will be listed here...</b></div>
上面的示例将使用GET方法调用getcourse.php。 getcourse.php文件加载catalog.xml。 getcourse.php如下所示-
<?php $q = $_GET["q"];
$xmlDoc = new DOMDocument();
$xmlDoc-?>load("catalog.xml");
$x = $xmlDoc->getElementsByTagName('COURSE');
for ($i = 0; $i<=$x->length-1; $i++) {
=
if ($x->item($i)->nodeType == 1) {
if ($x->item($i)->childNodes->item(0)->nodeValue == $q) {
$y = ($x->item($i)->parentNode);
}
}
}
$cd = ($y->childNodes);
for ($i = 0;$ilength;$i++) {
if ($cd->item($i)->nodeType == 1) {
echo("<b>" . $cd->item($i)->nodeName . ":</b> ");
echo($cd->item($i)->childNodes->item(0)->nodeValue);
echo("<br>");
}
}
?>
Catalog.xml
具有课程列表和详细信息的XML文件。此文件可通过getcourse.php访问
<catalog>
<subject>
<course>Android</course>
<country>India</country>
<company>TutorialsPoint</company>
<price>$10</price>
<year>2015</year>
</subject>
<subject>
<course>Html</course>
<country>India</country>
<company>TutorialsPoint</company>
<price>$15</price>
<year>2015</year>
</subject>
<subject>
<course>Java</course>
<country>India</country>
<company>TutorialsPoint</company>
<price>$20</price>
<year>2015</year>
</subject>
<subject>
<course>Microsoft</course>
<country>India</country>
<company>TutorialsPoint</company>
<price>$25</price>
<year>2015</year>
</subject>
</catalog>
它将产生以下结果-

类别:PHP 技巧、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!