XMLHttpRequest 와 더불어 유용할 것 같아서 만들었습니다.
역시 다른데 코드는 제 스타일도 아니고 읽기도 힘들고 그래서.. 후다닥 만들었다는..
recursive call 을 하기는 하지만 나름 쉽게 작성했습니다.
코드 가지고 태클은 금지.. ㅎㅎ
/*
* XMLParser Control Module
*
* @package
* @path
* @filename xmlparser.js
* @author
* @date 2010/05/03
*
* Change History
* Date Engineer Type Description
* ---------- ------------------ --------- --------------------
* 2010/05/03 Henry Jeong create initialize
*/
/*
*
* XMLParser module object
*
*/
var JXmlParser = function() {
};
/*
*
* xml parser variables
*/
JXmlParser.prototype.vars = {
parser : null,
doc : null,
xmldata : null,
root : null,
xml : null
}
JXmlParser.prototype.init = function() {
if (window.DOMParser) {
JXmlParser.vars.parser = new DOMParser();
JXmlParser.vars.doc = JXmlParser.vars.parser.parseFromString(
JXmlParser.vars.xmldata, "text/xml");
JXmlParser.vars.root = JXmlParser.vars.doc.childNodes.item(0);
} else {
JXmlParser.vars.doc = new ActiveXObject("Microsoft.XMLDOM");
JXmlParser.vars.doc.async = "false";
JXmlParser.vars.doc.loadXML(JXmlParser.vars.xmldata);
JXmlParser.vars.root = JXmlParser.vars.doc.documentElement;
}
JXmlParser.vars.xml = new Object();
JXmlParser.vars.xml["length"] = JXmlParser.vars.root.childNodes.length;
}
JXmlParser.prototype.setXml2Array = function(pobj, node) {
var len = node.length;
if (window.DOMParser) {
for ( var i = 0; i < len; i++) {
if (node[i].nodeName.charAt(0) == "#") {
continue;
}
pobj[i] = new Object();
pobj[i][node[i].nodeName] = new Object();
pobj[i][node[i].nodeName]["value"] = node[i].textContent;
pobj[i][node[i].nodeName]["length"] = node[i].childNodes.length;
pobj[i][node[i].nodeName]["attributes"] = new Object();
if (node[i].attributes && node[i].attributes.length > 0) {
var loop = node[i].attributes.length;
for ( var j = 0; j < loop; j++) {
pobj[i][node[i].nodeName]["attributes"][node[i].attributes[j].nodeName] = node[i].attributes[j].nodeValue;
}
}
if (node[i].childNodes.length > 0) {
JXmlParser.setXml2Array(pobj[i][node[i].nodeName],
node[i].childNodes, node[i].childNodes.length);
}
}
} else {
for ( var i = 0; i < len; i++) {
if (node[i].nodeName.charAt(0) == "#") {
continue;
}
pobj[i] = new Object();
pobj[i][node[i].nodeName] = new Object();
pobj[i][node[i].nodeName]["value"] = node[i].text;
pobj[i][node[i].nodeName]["length"] = node[i].childNodes.length;
pobj[i][node[i].nodeName]["attributes"] = new Object();
if (node[i].attributes && node[i].attributes.length > 0) {
var loop = node[i].attributes.length;
for ( var j = 0; j < loop; j++) {
pobj[i][node[i].nodeName]["attributes"][node[i].attributes[j].nodeName] = node[i].attributes[j].text;
}
}
if (node[i].childNodes.length > 0) {
JXmlParser.setXml2Array(pobj[i][node[i].nodeName],
node[i].childNodes, node[i].childNodes.length);
}
}
}
}
JXmlParser = new JXmlParser();
/*
* <script type="text/javascript"> <!-- var objDivDebug =
* document.getElementById("divDebug");
*
function run() {
JXmlParser.vars.xmldata = "<?xml version='1.0' encoding='UTF-8'?><parser><totalcount type='number'><total>A</total><total>B</total></totalcount><items name='user' type='image'><item><seq>1</seq><id><![CDATA[jjeong****]]></id><originalImage><![CDATA[http://www.naver.com]]></originalImage><viewImage><![CDATA[http://www.naver.com]]></viewImage><thumbImage><![CDATA[http://www.naver.com]]></thumbImage><cheerMessage><![CDATA[한국어 테스트.]]></cheerMessage><register><![CDATA[20100503175621]]></register></item><item><seq>2</seq><id><![CDATA[layd****]]></id><originalImage><![CDATA[http://www.naver.com]]></originalImage><viewImage><![CDATA[http://www.naver.com]]></viewImage><thumbImage><![CDATA[http://www.naver.com]]></thumbImage><cheerMessage><![CDATA[CHEER MESSAGE]]></cheerMessage><register><![CDATA[20100503175621]]></register></item></items></parser>";
JXmlParser.init();
JXmlParser.setXml2Array(JXmlParser.vars.xml, JXmlParser.vars.root.childNodes);
objDivDebug.innerHTML += JXmlParser.vars.xml.length + "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[0]["totalcount"].value + "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[0]["totalcount"].attributes.type + "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[0]["totalcount"].length + "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[0]["totalcount"][0]["total"].value + "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[0]["totalcount"][0]["total"].length + "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[0]["totalcount"][1]["total"].value + "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[0]["totalcount"][1]["total"].length + "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[1]["items"].value + "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[1]["items"].attributes.name + "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[1]["items"].length + "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[1]["items"][0]["item"] .length+ "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[1]["items"][0]["item"][0]["seq"] .value+ "<br>";
objDivDebug.innerHTML += JXmlParser.vars.xml[1]["items"][0]["item"][1]["id"] .value+ "<br>";
}
* //--> </script>
*/
*/