JS는 xml 파일 노드 및 속성을 읽고 출력합니다(IE, FF와 호환 가능)
이거 찾기가 힘들었는데 그래도 찾았네요.
이런 좋은 내용은 혼자만 간직할 수 없어서 공유하려고 올립니다 ^_^ |
book.html 코드는 다음과 같습니다.
++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++
<!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 ">
<머리>
<스크립트 언어="자바스크립트">
var xmlDoc;
var 찾아보기="";
함수 loadXML()
{
var fileRoute="books.xml"
if(window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Msxml2.DOMDocument');
xmlDoc.async=false;
xmlDoc.load(fileRoute);
찾아보기="ie";
}
else if(document.implementation && document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument('', '', null);
xmlDoc.load(fileRoute);
찾아보기="ff";
}
또 다른
{
Alert('이 브라우저와 호환되지 않습니다!');
}
}
함수 getmessage()
{
var msg='<table border="1" cellpacing="0" cellpadding="0" width="500">';
msg+='<tr><td width="90"></td><td width="100">도서 번호</td><td width="100">카테고리 번호</td><td width= "110">책 이름</td><td width="100">저자</td></tr>'
if(browse=="ff")
{
var cNodes = xmlDoc.getElementsByTagName("book");
for(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("저자")[j].childNodes[0].nodeValue;
msg+='<tr><td>'+j+'</td><td>'+bookID+'</td><td width="100">'+sortID+'</td><td width="190 ">'+bookTitle+'</td><td width="120">'+bookAuthor+'</td></tr>'
}
}
그렇지 않으면(browse=="ie")
{
var 상태 = xmlDoc.readyState;
if (상태 == 4)
{
var oNodes = xmlDoc.selectNodes("//books/book");
for(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 ">'+bookTitle+'</td><td width="120">'+bookAuthor+'</td></tr>'
}
}
}
msg+='</table>';
//경고(메시지)
document.getElementById("bookList").innerHTML=msg;
}
</script>
</head>
<body onload="loadXML();">
<div id="bookList" style="width:500px;">
</div>
<table border="0" cellpacing="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>
++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++
books.xml 코드는 다음과 같습니다.
++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
<?xml version="1.0" 인코딩="gb2312"?>
<책>
<책 id="4" sortID="a4">
<title>author4aa</title>
<author>author4</author>
</book>
<책 id="5" sortID="a5">
<title>author55</title>
<author>저자5</author>
</book>
<책 id="6" sortID="a6">
<title>책제목</title>
<author>저자6</author>
</book>
<책 id="7" sortID="a7">
<title>책제목</title>
<author>author7</author>
</book>
<책 id="8" sortID="a8">
<title>책제목</title>
<author>author8</author>
</book>
<book id="15" sortID="a15">
<title>author155</title>
<author>author15</author>
</book>
</books>