티스토리 뷰
먼저 xml 파일을 로드 하는것 부터 알아보자.
아래와 같이 로드 하는 방법이 있는데 IE에서는 잘되지만 파이어폭스, 크롬, 오페라, 사파리 에서는 잘 되지 않았다. 내가 뭔가를 잘 몰라서 그러는듯 하다. 지금까지 IE에서만 작업을 해오다 보니 타 브라우져에서는 어떻게 되는질 모르겠다.
var xmldom = null;
if(window.ActiveXObject)
{
xmldom = new ActiveXObject("Microsoft.XMLDOM");
alert(this._xmlDom);
}
else if(document.implementation && document.implementation.createDocument)
{
xmldom = document.implementation.createDocument("", "", null);
}
else
{
alert("Your browser cannot handle this script");
}
xmldom.async = false;
xmldom.load(filename);
그래서 XMLHTTP를 사용해 보았다.
var xmlDom = null;
var xmlHttp = null;
if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
else
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("get", filename, false);
xmlhttp.send("");
xmldom = xmlHttp.responseXML;
IE, 크롬, 파이어폭스, 사파리, 오패라 다 잘된다.. ㅋㅋ
이참에 앞으로의 귀찮은 일들을 사전에 미리 방지하고자 XMLDOM이라는 객체를 하나 만들어 보았다.
function XMLDOM()
{
this._xmlDom = null;
this._xmlHttp = null;
if (window.ActiveXObject)
{
this._xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
}
else
{
this._xmlHttp = new XMLHttpRequest()
}
}
XMLDOM.prototype.load = function(fileName)
{
this._xmlHttp.open("GET", fileName, false);
this._xmlHttp.send("");
this._xmlDom = this._xmlHttp.responseXML;
}
XMLDOM.prototype.selectNodes = function(xpath)
{
if(window.ActiveXObject)
{
return this._xmlDom.selectNodes(xpath);
}
else
{
var nodes = this._xmlDom.evaluate(xpath, this._xmlDom, null, XPathResult.ANY_TYPE, null);
var ret = new Array();
var node = nodes.iterateNext();
while(node)
{
ret[ret.length] = node;
node = nodes.iterateNext();
}
return ret;
}
}
XMLDOM.prototype.selectSingleNode = function(xpath)
{
return this.selectNodes(xpath)[0];
}
사용법은 다음과 같다.
var xmlDom = new XMLDOM();
xmlDom.load(파일명);
var xpath = "//books/book[@title='abcd']":
var nodes = xmlDom.selectNodes(xpath);
for(var i=0; i < nodes.length; i++)
{
document.write(nodes[i].childnodes[0].nodevalue);
}
'프로그래밍 > Javascript' 카테고리의 다른 글
| event를 발생 시킨 object 알아 내기 (0) | 2010.05.14 |
|---|---|
| Window 크기 구하기 (0) | 2010.04.21 |
| 사용자 정의 속성(attribute) (?) (0) | 2009.11.16 |
| 내가 자주 사용하는 javascript 함수 및 객체 모음 (1) | 2009.07.14 |
| 사용자 정의 오류 (0) | 2009.07.14 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
