Javascript is compatible with various browsers to parse XML document data.

Source: Internet
Author: User

Javascript is compatible with various browsers to parse XML document data.

This document describes how to parse XML document data in Javascript compatible with browsers. Share it with you for your reference. The specific analysis is as follows:

Many documents on the website that Parse XML documents using JS are more or less problematic,

The following code is used to parse XML documents and be compatible with various browsers.

ParseXMLDOM. js code:

?

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

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

/*

* Parse XML documents in pure JS (compatible with various browsers)

*/

Function parseXMLDOM (){

Var _ browserType = "";

Var _ xmlFile = "";

Var _ XmlDom = null;

Return {

"GetBrowserType": function (){

Return _ browserType;

},

"SetBrowserType": function (browserType ){

_ BrowserType = browserType;

},

"GetXmlFile": function (){

Return _ xmlFile;

},

"SetXmlFile": function (xmlFile ){

_ XmlFile = xmlFile;

},

"GetXmlDom": function (){

Return _ XmlDom;

},

"SetXmlDom": function (XmlDom ){

_ XmlDom = XmlDom;

},

"GetBrowserType": function (){

Var browserType = "";

If (navigator. userAgent. indexOf ("MSIE ")! =-1 ){

BrowserType = "IE ";

} Else if (navigator. userAgent. indexOf ("Chrome ")! =-1 ){

BrowserType = "Chrome ";

} Else if (navigator. userAgent. indexOf ("Firefox ")! =-1 ){

BrowserType = "Firefox"

}

Return browserType;

},

"CreateXmlDom": function (xmlDom ){

If (this. getBrowserType () = "IE") {// IE browser

XmlDom = new ActiveXObject ('Microsoft. XMLDOM ');

XmlDom. async = false;

XmlDom. load (this. getXmlFile ());

} Else {

Var xmlhttp = new XMLHttpRequest ();

Xmlhttp. open ("GET", this. getXmlFile (), false );

Xmlhttp. send (null );

XmlDom = xmlhttp. responseXML;

}

Return xmlDom;

},

"ParseXMLDOMInfo": function (){

Var xmlDom = this. getXmlDom ();

If (this. getBrowserType () = "IE "){

Var bookObj = xmlDom. selectNodes ("books/book ");

If (typeof (bookObj )! = "Undifined "){

Var strHtml = "";

For (var I = 0; I <bookObj. length; I ++ ){

StrHtml + = bookObj [I]. selectSingleNode ("isbn"). text;

StrHtml + = "";

StrHtml + = bookObj [I]. selectSingleNode ("price"). text;

StrHtml + = "";

StrHtml + = bookObj [I]. selectSingleNode ("title"). text;

If (I! = BookObj. length-1 ){

StrHtml + = "<br> ";

}

}

}

} Else {

Var book = xmlDom. getElementsByTagName ("book ");

Var strHtml = "";

For (var I = 0; I <book. length; I ++ ){

StrHtml + = book [I]. getElementsByTagName ("isbn") [0]. textContent;

StrHtml + = "";

StrHtml + = "";

StrHtml + = book [I]. getElementsByTagName ("price") [0]. textContent;

StrHtml + = "";

StrHtml + = book [I]. getElementsByTagName ("title") [0]. textContent;

If (I! = Book. length-1 ){

StrHtml + = "<br> ";

}

}

}

Document. getElementById ("msg"). innerHTML = strHtml;

}

}

}

Window. onload = function (){

Var parseObj = new parseXMLDOM ();

// Set the browser type

ParseObj. setBrowserType (parseObj. getBrowserType ());

// Set the file path

ParseObj. setXmlFile ("test. xml ");

// Create XMLDOM

ParseObj. setXmlDom (parseObj. createXmlDom (null ));

// Parse XMLDOM

ParseObj. parseXMLDOMInfo ();

}

Index.html code:

?

1

2

3

4

5

6

7

8

9

10

11

<! DOCTYPE html>

<Html>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">

<Title> parse data in XML documents by JS (compatible with all browsers) </title>

<Script language = "javascript" type = "text/javascript" src = "js/parseXMLDOM. js"> </script>

</Head>

<Body>

<Span id = "msg"> </span>

</Body>

</Html>

Test. xml code:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<? Xml version = "1.0" encoding = "UTF-8"?>

<Books>

<Book>

<Isbn> 2207-1258-123 </isbn>

<Price> 25 </price>

<Title> Javascript </title>

</Book>

<Book>

<Isbn> 2207-1258-456 </isbn>

<Price> 50 </price>

<Title> Ajax </title>

</Book>

<Book>

<Isbn> 2207-1258-789 </isbn>

<Price> 75 </price>

<Title> C # </title>

</Book>

</Books>

I hope this article will help you design javascript programs.

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.