Handling XML Data for Ajax - Populating the Form on Other Browsers
(Page 5 of 5 )
The code for handling non-Microsoft browsers, shown in Example 4-7, is similar; there are some minor differences in the parser API, but that's about it.
Example 4-7. The nonMSPopulate( ) function
1 function nonMSPopulate() {
2 var resp = req.responseText;
3 var parser = new DOMParser();
4 var dom = parser.parseFromString(resp,"text/xml");
5
6 decVal = dom.getElementsByTagName("decimal");
7 var decimal = document.getElementById('decimal');
8 decimal.value=decVal[0].childNodes[0].nodeValue;
9
10 hexVal = dom.getElementsByTagName("hexadecimal");
11 var hexadecimal = document.getElementById('hexadecimal');
12 hexadecimal.value=hexVal[0].childNodes[0].nodeValue;
13
14 octVal = dom.getElementsByTagName("octal");
15 var octal = document.getElementById('octal');
16 octal.value=octVal[0].childNodes[0].nodeValue;
17
18 hyperVal = dom.getElementsByTagName("hyper");
19 var hyper = document.getElementById('hyper');
20 hyper.value=hyperVal[0].childNodes[0].nodeValue;
21
22 binaryVal = dom.getElementsByTagName("binary");
23 var bin = document.getElementById('bin');
24 bin.value=binaryVal[0].childNodes[0].nodeValue;
25 }
We create a newDOMParser(line 3), then we create a DOM on line 4 from the XML string that we received from the servlet. Next, we get the element between the<decimal></decimal>tags (line 6) by callingdom.getElementByTagName("decimal"). After retrieving thedecimalform element from our HTML document, we retrieve the value sent to us in the XML and use it to set the appropriate field:
decimal.value=decVal[0].childNodes[0].nodeValue;
The reference todecVal[0]gets the data between the<decimal></decimal>tags. If we had two sets of<decimal></decimal>tags, we would reference the second set asdecVal[1].
This is what the data should look like in the response sent from the servlet:
<converted-values>
<decimal>97</decimal>
<hexadecimal>0x61</hexadecimal>
<octal>0141</octal>
<hyper>&0x61;</hyper>
<binary>1100001B</binary>
</converted-values>
Please check back next week for the conclusion to this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the book Ajax on Java, written by Steven Douglas Olson (O'Reilly, 2007; ISBN: 0596101872). Check it out today at your favorite bookstore. Buy this book now.
|
|