XML reading method for XML file operations using JavaScript

Source: Internet
Author: User

XML reading method for XML file operations using JavaScript

This document describes how to read XML files in JavaScript. Share it with you for your reference. The specific analysis is as follows:

Suppose we want to read the following info. xml file.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<? Xml version = "1.0" encoding = "gb2312"?>

<Root>

<Data id = "1">

<Name> ceun </name>

<Age> 21 </age>

</Data>

<Data id = "2">

<Name> <! [CDATA [James]> </name>

<Age> 22 </age>

</Data>

<Data id = "3">

<Name> jake </name>

<Age> 23 </age>

</Data>

<Data id = "4">

<Name> hello </name>

<Age> 20 </age>

</Data>

<Data id = "5">

<Name> Paul </name>

<Age> 25 </age>

</Data>

</Root>

Next, read and traverse info. xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">

<HTML>

<HEAD>

<TITLE> Hello </TITLE>

</HEAD>

<BODY>

<Script language = "JavaScript">

<! --

// Generate an XML object.

Function createXMLDom (){

If (window. ActiveXObject)

Var xmldoc = new ActiveXObject ("Microsoft. XMLDOM ");

Else

If (document. implementation & document. implementation. createDocument)

Var xmldoc = document. implementation. createDocument ("", "doc", null );

Xmldoc. async = false;

// In the same way as FireFox, the value cannot be set to False;

Xmldoc. preserveWhiteSpace = true;

Return xmldoc;

}

// Load the XML file.

Var xmlDom = createXMLDom ();

XmlDom. load ("info. xml ");

// Obtain the root node

Var root=xmlDom.doc umentElement;

Var data = "";

Var names = root. getElementsByTagName ("name ");

Var ages = root. getElementsByTagName ("age ");

Var len = names. length;

For (var I = 0; I <len; I ++ ){

Data + = "name :";

Data + = names [I]. firstChild. nodeValue;

Data + = "Age :";

Data + = ages [I]. firstChild. nodeValue;

Data + = "";

}

Alert (data );

-->

</SCRIPT>

</BODY>

</HTML>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.