xml & JavaScript > script

본문 바로가기
사이트 내 전체검색

script

xml & JavaScript

페이지 정보

작성자 서방님 댓글 0건 조회 80회 작성일 07-08-08 10:44

본문

XML is a very important base on which Web Services work, and, in conjunction with a number of client- and server-side languages, can be put to good effect.

Let's see how we can use XML and client side JavaScript to display the contents of a XML file, access child elements, manipulate elements, and more!

Browser Issues
When it comes to client side languages, browser incompatibilities are a major issue. But here, using XML and JavaScript, it's XML that's the issue: not all browsers support the parsing of XML documents.

I'll use IE6 to explain the codes. Browsers that don't support XML can't read these, so when you view an XML file in such a browser, it will simply ignore all the tags.

Sample XML File
Let's consider a sample XML file, which shows employee data and Turnover of a company:

<?xml version="1.0" ?>
<company>
<employee id="001" sex="M" age="19">Premshree Pillai</employee>
<employee id="002" sex="M" age="24">Kumar Singh</employee>
<employee id="003" sex="M" age="21">Ranjit Kapoor</employee>
<turnover>
<year id="2000">100,000</year>
<year id="2001">140,000</year>
<year id="2002">200,000</year>
</turnover>
</company>

Manipulating the XML file data using JavaScript
Load The XML File

You can load a XML fie from JavaScript like this:

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
function loadXML(xmlFile)
{
 xmlDoc.async="false";
 xmlDoc.onreadystatechange=verify;
 xmlDoc.load(xmlFile);
 xmlObj=xmlDoc.documentElement;
}

Actually, just the last two lines of the function are enough to load the XML file. The previous two lines ensure that any JavaScript functions that we may use to manipulate the XML file data later, will not perform any function on an uninitialized object. Thus the function verify()is called:

function verify()
{
 // 0 Object is not initialized
 // 1 Loading object is loading data
 // 2 Loaded object has loaded data
 // 3 Data from object can be worked with
 // 4 Object completely initialized
 if (xmlDoc.readyState != 4)
 {
   return false;
 }
}

Now the XML file can be loaded:

loadXML('xml_file.xml');

Display The Contents of the XML File

View the entire contents of the XML file using alert(xmlObj.xml); The whole XML file will be displayed in an alert box as it is, with proper indentation.

Children and Nodes

In the above XML file, <company> is the top level tag under which all other tags fall. These tags are called children. This XML file can be represented graphically like a folder-tree:

 

In the above XML file, the top level tag <company> has 4 children.

The numbering of children (as is usual in all languages) starts from 0 (zero). The <turnover> tag has 3 children under it.

We can find the number of children a tag has by using the childNodes.length property. Thus the number of children of <company> tag (here, 4) can be found by using xmlObj.childNodes.length

The number of children of <turnover> tag (here, 3) can be found by using xmlObj.childNodes(3).childNodes.length

Here we use childNodes(3) because <turnover> is the 3rd child of <company>

Test for Children

You can test whether a particular node child has any children using childNodes(i).hasChildNodes

Thus, xmlObj.childNodes(3).hasChildNodes() will return true. xmlObj.childNodes(2).hasChildNodes() will return false, as the <employee> tag doesn't have any children.

Get Tag Name

You can get the tag name of a child using childNodes(i).tagName. Thus, xmlObj.tagName will return "company". xmlObj.childNodes(0).tagName will return "employee". xmlObj.childNodes(3).childNodes(0).tagName will return "year".

Display the Content of a Tag

In the XML file, the content of the 1st <employee> tag is "Premshree Pillai". You can get this value using xmlObj.childNodes(0).firstChild.text

xmlObj.childNodes(2).firstChild.text will return "Suhasini Pandita". Similarly, xmlObj.childNodes(3).childNodes(1).firstChild.text will return "140,000".

Attributes

In the XML file, the <employee> tag has 3 attributes. An attribute can be accessed using childNodes(i).getAttribute("AttributeName"). Thus, xmlObj.childNodes(0).getAttribute("id") will return "001". xmlObj.childNodes(1).getAttribute("age") will return "24". And xmlObj.childNodes(2).getAttribute("sex") will return "F".

Project: An XML-Based JavaScript Ticker

댓글목록

등록된 댓글이 없습니다.

Total 846건 40 페이지
script 목록
번호 제목 글쓴이 조회 날짜
261 서방님 85 08-08
260 서방님 101 08-08
259 서방님 137 08-08
258 서방님 102 08-08
257 서방님 155 08-08
열람중 서방님 81 08-08
255 서방님 429 08-08
254 서방님 325 08-08
253 서방님 80 08-08
252 서방님 189 08-08
251 서방님 190 08-08
250 서방님 79 08-08
249 서방님 146 08-08
248 서방님 202 08-08
247 서방님 163 08-08
게시물 검색

회원로그인

접속자집계

오늘
22
어제
302
최대
1,347
전체
155,083
Latest Crypto Fear & Greed Index

그누보드5
Copyright © 서방님.kr All rights reserved.