我有一个XML文件。我需要在“ Grad”选项上显示来自Firma和BrojTelefona属性的值,位于Option列表的右侧。作为文本。
HTML:
<html>
<form method="post" action="">
<?php
echo "<select>";
$xml = simplexml_load_file('adler.xml');
foreach ($xml->record as $item)
{
echo "<option value='".$item->Grad."'>" . $item->Grad . "</option>";
}
echo "</select>";
?>
</form>
</html>
我的XML文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<record>
<Firma>TD electronic</Firma>
<BrojTelefona>034 715 445</BrojTelefona>
<Grad>Arandjelovac</Grad>
</record>
<record>
<Firma>Lihno d.o.o.</Firma>
<BrojTelefona>011 848 5705</BrojTelefona>
<Grad>Batajnica</Grad>
</record>
答案 0 :(得分:0)
我找到了解决方法。
<html>
<head>
<script>
function show(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 (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","get.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<?php
echo '<select onchange="show(this.value)">';
$xml = simplexml_load_file('adler.xml');
foreach ($xml->record as $item)
{
echo "<option value='".$item->Grad."'>" . $item->Grad . "</option>";
}
echo "</select>";
?>
</form>
<div id="txtHint"><b>Servis Adler uređaja za </b></div>
</body>
</html>
和get.php文件
<?php
$q=$_GET["q"];
$xmlDoc = new DOMDocument();
$xmlDoc->load("adler.xml");
$x=$xmlDoc->getElementsByTagName('Grad');
for ($i=0; $i<=$x->length-1; $i++) {
//Process only element nodes
if ($x->item($i)->nodeType==1) {
if ($x->item($i)->childNodes->item(0)->nodeValue == $q) {
$y=($x->item($i)->parentNode);
}
}
}
$record=($y->childNodes);
for ($i=0;$i<$record->length;$i++) {
//Process only element nodes
if ($record->item($i)->nodeType==1) {
echo("<b>" . $record->item($i)->nodeName . ":</b> ");
echo($record->item($i)->childNodes->item(0)->nodeValue);
echo("<br>");
}
}
?>