Javascript DOM parser

1] XML parsing and storing values in array
2] XML parsing and storing values in variable

1] XML parsing and storing values in array
  
   index.jsp


 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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript xml Parsing</title>

</head>
<body>
 Press F12 to see the output in console.
 <br> Note: console keyword is not supported by IE8, so comment
 those lines

 <script type="text/javascript">
  var xml = "<NewDataSet xmlns=\"\">  <start> <Sm_Scheme_Id>10</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.5000</Sm_Buy_Brok> <Sm_Sell_Brok>0.5000</Sm_Sell_Brok> </start>   <start> <Sm_Scheme_Id>1</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.0050</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </start>   <start> <Sm_Scheme_Id>11</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>0.3500</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </start>   <start> <Sm_Scheme_Id>12</Sm_Scheme_Id> <Sm_trd_Type>TRD</Sm_trd_Type> <Sm_Buy_Brok>1.0000</Sm_Buy_Brok> <Sm_Sell_Brok>0.0000</Sm_Sell_Brok> </start>  </NewDataSet>";

  alert("xml is : " + xml);

  if (window.DOMParser) // Mozilla,Chrome etc.
  {
   parser = new DOMParser();
   xmlDoc = parser.parseFromString(xml, "text/xml");
  } else // Internet Explorer
  {
   xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
   xmlDoc.async = false;
   xmlDoc.loadXML(xml);
  }

  var x = xmlDoc.getElementsByTagName("start"); //1st checks how many <start> tag is present, then iterates that many no. of times. Inside <start> </start> tag are all the remaining tags
  alert(x.length);
  var key = "";
  var values = "";
  for (i = 0; i < x.length; i++) {
   var ret = [];
   var tot = [];
   ret[0] = x[i].getElementsByTagName("Sm_Scheme_Id")[0].childNodes[0].nodeValue;
   key += ret[0] + " ";

   ret[1] = x[i].getElementsByTagName("Sm_trd_Type")[0].childNodes[0].nodeValue;
   ret[2] = x[i].getElementsByTagName("Sm_Buy_Brok")[0].childNodes[0].nodeValue;
   ret[3] = x[i].getElementsByTagName("Sm_Sell_Brok")[0].childNodes[0].nodeValue;

   tot.push(ret);
   console.log("tot=" + tot);
   values += tot + "|";

  }
 </script>

</body>
</html>

Output: (In browser's console)
tot=10,TRD,0.5000,0.5000
tot=1,TRD,0.0050,0.0000
tot=11,TRD,0.3500,0.0000
tot=12,TRD,1.0000,0.0000



2] XML parsing and storing values in variable

  index.jsp


 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
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Javascript DOM Parser</title>

<script>

var xml = "<parent><tag><string>rahul__1</string><string>abcde__1</string></tag><tag><string>rahul__2</string><string>abcde__2</string></tag></parent>";
                    alert("xml is : "+xml);
                    if (window.DOMParser)  // Mozilla,Chrome etc.
                    {
                    parser=new DOMParser();
                    xmlDoc=parser.parseFromString(xml,"text/xml");
                    }
                    else // Internet Explorer
                    {
                    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc.async=false;
                    xmlDoc.loadXML(xml);
                    }
                   
                    var String_1 = xmlDoc.getElementsByTagName("string")[0].childNodes[0].nodeValue;
                    var String_2 =xmlDoc.getElementsByTagName("string")[1].childNodes[0].nodeValue;
                   
                    var String_3 = xmlDoc.getElementsByTagName("string")[2].childNodes[0].nodeValue;
                    var String_4 =xmlDoc.getElementsByTagName("string")[3].childNodes[0].nodeValue;
                   
                                       
                    alert("1st String :   "+String_1);
                    alert("2nd String :  "+String_2);
                   
                    alert("3rd String :  "+String_3);
                    alert("4th String  : "+String_4);
                   
                   
</script>

</head>
<body>   

</body>
</html>

No comments:

Post a Comment