<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script>

/*
사용방법.
var xmlDoc = new rssXmlParser(req.responseXML);
xmlDoc["title"] ;
xml.item[0]["title"]
*/

function rssXmlParser(xml){
//node
var getNode =function (obj, tag){
return obj.getElementsByTagName(tag)[0];
}

//node value
var getValue = function (obj){
try{
return obj.firstChild.nodeValue;
} catch(e){
return null;
}
}
//node item
var getItem = function (obj){
var item = new Object();
var objLength = obj.childNodes.length;

for(var j =0;j <objLength ; j++){
var eleItem = obj.childNodes[j];
item[eleItem.nodeName] =getValue(eleItem);
}
return item;
}

var xmlParseData = new Object();
var item_list = new Array();
var channel = getNode(xml, "channel");
var channelLength = channel.childNodes.length;

for(var i =0;i <channelLength ; i++){

var sNode = channel.childNodes[i];

if(sNode.childNodes.length >1){
var pData = getItem(sNode);

if(sNode.nodeName== "image"){
xmlParseData.image = pData;
}else{
item_list.push(pData);
}
}else{
xmlParseData[sNode.nodeName] =getValue(sNode);
}

}
xmlParseData.item = item_list;

return xmlParseData;

}

function testF(){
var requestReq;
var cnt = 0;
var xml = new Array();
//xml[cnt++] = "MSXML2.XMLHTTP.5.0";
xml[cnt++] = "MSXML2.XMLHTTP.4.0";
xml[cnt++] = "MSXML2.XMLHTTP.3.0";
xml[cnt++] = "MSXML2.XMLHTTP";
xml[cnt++] = "Microsoft.XMLHTTP";
xml[cnt++] = "WinHttp.WinHttpRequest.5";
xml[cnt++] = "WinHttp.WinHttpRequest.5.1";

if (window.ActiveXObject) {
for (var i=0; i<xml.length; i++) {
try {
requestReq = new ActiveXObject(xml[i]);
break;
} catch(e) {
requestReq = null;
}
}
} else if(window.XMLHttpRequest) {
try {
requestReq = new XMLHttpRequest();
} catch(e) {
requestReq = null;
}
}

requestReq.open("POST", "http://blog.rss.naver.com/qmffjem09.xml", true);

requestReq.onreadystatechange = function(){
try{
if(requestReq.readyState==4){
if(requestReq.status==200){
rssXmlView(requestReq);
}
}
}catch(e){
alert(e.name+"\n"+e.description);

}

};// function_name;
requestReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
requestReq.send("");
}

function rssXmlView(requestReq){

var obj = rssXmlParser(requestReq.responseXML);

// 사용법.

alert(obj["title"] +"\n"+obj.item[0]["title"])
}
</script>
</HEAD>

<BODY>
<input type ="button" value = "rssXml파싱" onclick ="testF()">
</BODY>
</HTML>

2011/10/11 12:09 2011/10/11 12:09

Trackback Address :: 이 글에는 트랙백을 보낼 수 없습니다