JS liest und gibt XML -Dateiknoten und -attribute aus (kompatibel mit IE, FF)
Es war schwer für mich, dieses Ding zu finden, aber ich fand es trotzdem.
Ich kann dieses gute Zeug nicht für mich behalten, also poste ich es, um es zu teilen, ^ _ ^ |
Book.html Code lautet wie folgt:
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns=" http://www.w3.org/1999/xhtml ">
<Kopf>
<script language="javascript">
var xmlDoc;
var browse = "";
Funktion loadxml ()
{
var fileroute = "books.xml"
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
xmlDoc.async=false;
xmldoc.load (fileroute);
Browse = "IE";
}
sonst wenn (document.implementation && document.implementation.createDocument)
{
xmldoc = document.implementation.createcument ('', '', null);
xmldoc.load (fileroute);
Browse = "ff";
}
anders
{
alarm ('nicht mit diesem Browser kompatibel!');
}
}
Funktion getMessage ()
{
var msg = '<table border = "1" cellspacing = "0" cellpadding = "0" width = "500">';
msg+= '<tr> <td width = "90"> </td> <td width = "100"> Buchnummer </td> <td width = "100"> Kategoriezahl </td> <td width = "110"> Buchname </td> <td width = "100"> Autor </td> </tr> '
if (Browse == "ff")
{
var cnodes = xmldoc.getElementsByTagName ("Buch");
für (j = 0; j <cnodes.length; j ++)
{
var bookId = xmldoc.getElementsByTagName ("book") [j] .getAttribute ("id");
var sortid = xmldoc.getElementsByTagName ("book") [j] .GetAttribute ("sortid");
var booktitle = xmldoc.getElementsByTagName ("title") [j] .Childnodes [0] .nodeValue;
var bookAuthor = xmldoc.getElementsByTagName ("Autor") [j] .Childnodes [0] .nodeValue;
msg+= '<tr> <td>'+j+'</td> <td>'+bookId+'</td> <td width = "100">'+sortid+'</td> <td width = "190 "> '+boottitle+' </td> <td width =" 120 "> '+bookAuthor+' </td> </tr> '
}
}
sonst wenn (Browse == "dh")
{
var state = xmldoc.readystate;
if (Zustand == 4)
{
var oneodes = xmldoc.selectnodes ("// books/book");
für (j = 0; j <onodes.length; j ++)
{
var bookId = onodes [j] .GetAttribute ("id");
var sortid = onodes [j] .getAttribute ("sortid");
var booktitle = onodes [j] .Childnodes [0] .Text;
var bookAuthor = ONODES [j] .Childnodes [1] .Text;
msg+= '<tr> <td>'+j+'</td> <td>'+bookId+'</td> <td width = "100">'+sortid+'</td> <td width = "190 "> '+boottitle+' </td> <td width =" 120 "> '+bookAuthor+' </td> </tr> '
}
}
}
msg+= '</table>';
// alarm (msg)
document.getElementById ("Buchliste"). InnerHtml = msg;
}
</script>
</head>
<body onload = "loadxml ();">
<div id = "booklist" style = "width: 500px;">
</div>
<table border = "0" cellspacing = "0" cellpadding = "0" width = "500">
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<input name = "button" type = "button" onclick = "getMessage ()" value = "onclick" />
</body>
</html>
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++
Der Code von books.xml lautet wie folgt:
++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
<?xml version="1.0"kodierung="gb2312"?>
<Bücher>
<book id = "4" sortid = "a4">
<title> Author4aa </title>
<autor> Author4 </Author>
</book>
<book id = "5" sortid = "a5">
<title> Author55 </title>
<autor> Author5 </Author>
</book>
<book id = "6" sortid = "a6">
<title> boottitle </title>
<autor> Author6 </Author>
</book>
<book id = "7" sortid = "a7">
<title> boottitle </title>
<autor> Author7 </Author>
</book>
<book id = "8" sortid = "a8">
<title> boottitle </title>
<autor> Author8 </Author>
</book>
<book id = "15" sortid = "a15">
<title> Author155 </title>
<autor> Author15 </Author>
</book>
</books>