> Hello World !!!

     

@syaku

XPath 를 이용한 selectSingleNode 사용하기 : 자바스크립트

 

XPath 표현식을 이용한 XML 데이터 검색에 유용하게 사용되는 selectSingleNode 함수입니다.
XPath 는 브라우져마다 처리방식이 조금식 다릅니다. 그래서 이를 크로스브라우징하게 처리하였습니다.

문제점. (추후 수정)

객체를 리턴하는 것이 아닌 해당 text 값을 리턴합니다.
네임스페이스는 테스트하지 못했습니다.
속성 노드는 기본 1개이하만 익식합니다. 그 이상 테스트하지 못했습니다.
예) /root/test[@name='aaa']
<root><test name="aaa">테스트</test></root>

 

XPath 표현식

/message/code[@id='title']/content 는?
/루트 message 노드 /자식 code 노드 [@ id속성의 값은 title] / 자식 content 노드

 

Javascript Source : action.js

'// Javascript Action Framework
'// programmed by 최석균
'// http://www.cyworld.com/syaku
'// http://syaku.tistory.com

// 브라우져
var _xa_browser = getBrowser();

/**
 * @ func getBrowser() { return int }
 * @brief 인터넷 브라우져를 int 리턴한다.
 */
function getBrowser() {
  // 0 : MSIE , 1 : Firefox , 2 : Safari , 3 : Chrome , 4 : Opera , 5 : Netscape
  var browser = ["MSIE","Firefox","Safari","Chrome","Opera","Netscape"];
  var agent = navigator.userAgent;

  for (var i in browser) {
    if (agent.indexOf(browser[i]) != -1) { return i; }
  }

  return null;
}

/**
 * @func xmlDoc(string) { return object }
 * @brief xml 문서를 읽어옴
 */
function xmlDoc(path) {
  var objXml;

  switch (_xa_browser) {
    case "0" :
    case "1" :
      objXml = CreateXmlDocument();
      objXml.async = false;
      objXml.load(path);
      return objXml.documentElement;
    break;
    default :
      objXml = CreateXmlHttpRequest();
      objXml.open("GET",path,false);
      objXml.send(null);
      return objXml.responseXML.documentElement;
    break;
  }

  return null;
}

/**
 * @func CreateXmlDocument() { return object }
 * @brief DOM 객체를 호출함.
 */
function CreateXmlDocument() {

  if (document.implementation && document.implementation.createDocument) {
    return document.implementation.createDocument("","",null);
  } else if (typeof ActiveXObject != "undefined") {
    try {
      return new ActiveXObject("Msxml2.DOMDocument");
    }
    catch (e) {
      return new ActiveXObject("Msxml.DOMDocument");
    }
  }
  return null;
}

/**
 * @func CreateXmlHttpRequest() { return object }
 * @brief XMLHTTP 객체를 호출함.
 */
function CreateXmlHttpRequest() {
  if (window.ActiveXObject) {
    try {
      return new ActiveXObject("Microsoft.XMLHTTP"); 
    } catch (e) {
      return new ActiveXObject("Msxml.XMLHTTP");
    }
  }
  else if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
  else { return null; }
}

/**
 * @func xSingleNodeValue(string,string) { return string }
 * @brief XPath 표현식을 이용하여, XML 노드를 검색합니다.
 */
function xSingleNodeValue(path,nodes) {
  var xdoc = xmlDoc(path);
  if (_xa_browser == "0") { return xdoc.selectSingleNode(nodes).text; }

  var node = nodes.replace(/^\//,"").split("/");
  var reg_attr = /\[@([A-Za-z0-9_-]+)=\'(.*)\'\]/

  for (var i in node) {

    var node_name = node[i];

    if (i == 0) {
      if (xdoc.nodeName != node_name) { return false; }
      continue;
    }

    var attr_name = null;
    var attr_value = null;

    if (reg_attr.test(node_name) == true) {
      reg_attr.exec(node_name);
      attr_name = RegExp.$1;
      attr_value = RegExp.$2;

      node_name = node_name.replace(reg_attr,"");
    }

    var cnt = xdoc.childNodes.length;

    for (var x=1; x < cnt; x++)
    {
      var node_child = xdoc.childNodes.item(x);
      if (node_child.nodeName != "#text" && node_child.nodeName == node_name) {

        if (!isNull(attr_name)) {
          var attr_doc = xdoc.getElementsByTagName(node_name);
          var attr_cnt = attr_doc.length;

          for (var a = 0; a < attr_cnt; a++) {
            if (attr_doc[a].getAttribute(attr_name) == attr_value) { xdoc = attr_doc[a]; }
          }
        } else { xdoc = node_child; }

        break;
      }
    }

  }

   if (xdoc.childNodes.length == 1) { return xdoc.firstChild.nodeValue; }
   else { return xdoc.childNodes.item(1).nodeValue; }
}

XML Source : test.xml

<?xml version="1.0" encoding="EUC-KR" ?>
<message>
  <code id="url">
    <content>http://syaku.springnote.com</content>
    <action>document.location.reload();</action>
  </code>
  <code id="title">
    <content>샤쿠님의 노트</content>
    <action>document.location.reload();</action>
  </code>
</message>

Example Source

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="ko" xml:lang="ko">
  <head>
    <title>테스트</title>
    <script type="text/javascript" src="./action.js"></script>
  </head>
  <body>
    <div>
      <h1>selectSingleNode</h1>
      <script type="text/javascript">
        //<![CDATA[
        // xSingleNodeValue("xml 파일 경로","찾을 노드")
          document.write(xSingleNodeValue("/test.xml","/message/code[@id='title']/content"));
        //]]>
      </script>
    </div>
  </body>
</html>

written by Seok Kyun. Choi. 최석균.
http://www.cyworld.com/syaku
http://syaku.tistory.com