XPath 표현식을 이용한 XML 노드 검색 : selectSienleNode
728x90
반응형

XPath 표현식을 이용하여 XML 데이터 검색이 유용한 selectSingleNode 를 크로스브라우징하게 제공되는 함수 입니다. 기존 방식과 다르게 해당 노드의 text(value)를 리턴합니다.
XPath 표현식
/message/code[@id='title']/content 는?
/루트 message 노드 /자식 code 노드 [@ id속성의 값은 title] / 자식 content 노드
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
728x90
반응형
'개발노트 > JavaScript' 카테고리의 다른 글
| XMLDOM 을 JSON 으로 파싱하여 활용하기, xml2json : 자바스크립트 (0) | 2009.07.16 |
|---|---|
| XPath selectSingleNode : 자바스크립트 (0) | 2009.05.25 |
| 폼 전송 일괄처리 : submit 전송 입력 폼 예제 (1) | 2009.05.25 |
| JSON : 자바스크립트 (0) | 2009.04.18 |