Transcript
Page 1: Making and Reading from XML Files

CSD 340 (Blum) 1

Making and Reading from XML Files

Chapter 14 of Beginning JavaScript (Paul Wilton)

Page 2: Making and Reading from XML Files

CSD 340 (Blum) 2

File/New/File

Page 3: Making and Reading from XML Files

CSD 340 (Blum) 3

Choose an XML File template and click Open

Page 4: Making and Reading from XML Files

CSD 340 (Blum) 4

Start of XML File

Page 5: Making and Reading from XML Files

CSD 340 (Blum) 5

Design tags and enter data.

Page 6: Making and Reading from XML Files

CSD 340 (Blum) 6

In the Data view

Page 7: Making and Reading from XML Files

CSD 340 (Blum) 7

Save file.

Page 8: Making and Reading from XML Files

CSD 340 (Blum) 8

Saving File

Page 9: Making and Reading from XML Files

CSD 340 (Blum) 9

Enter more data and save

Page 10: Making and Reading from XML Files

CSD 340 (Blum) 10

State Capital Quiz design

Page 11: Making and Reading from XML Files

CSD 340 (Blum) 11

Code for reading XML file for State data and initializing arrays

Page 12: Making and Reading from XML Files

CSD 340 (Blum) 12

Declare and instantiate arrays for state names and capitals

var QUIZ_SIZE=5;

var MyStateName = new Array(50);

var MyStateCapital = new Array(50);

Page 13: Making and Reading from XML Files

CSD 340 (Blum) 13

//loads data from xml file into the arrays (IE only)

function LoadStateArray()

{

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false;

xmlDoc.load("States.xml");

Declare and instantiate and ActiveX that can parse an XML file.

Setting the async property to false means that the code has to wait for a response. Since the next line of code is about loading the xml file, that is what we are waiting for.

Load the XML file.

Page 14: Making and Reading from XML Files

CSD 340 (Blum) 14

Webopedia definition of ActiveX

Page 15: Making and Reading from XML Files

CSD 340 (Blum) 15

Webopedia definition of asynchronous

Page 16: Making and Reading from XML Files

CSD 340 (Blum) 16

for(i=0; i< xmlDoc.getElementsByTagName("state").length; i++) { MyStateName[i] = xmlDoc.getElementsByTagName("state")

[i].getElementsByTagName("stateName")[0].firstChild.nodeValue; MyStateCapital[i] = xmlDoc.getElementsByTagName("state")

[i].getElementsByTagName("stateCapital")[0].firstChild.nodeValue; } //end of for loop} //end of function

Loop through the “state” elements.

Initialize the elements of the arrays from the data in the XML file.

Page 17: Making and Reading from XML Files

CSD 340 (Blum) 17

Whatis definition of tree

Page 18: Making and Reading from XML Files

CSD 340 (Blum) 18

References

• Beginning JavaScript, Paul Wilton

• http://www.webopedia.com

• http://www.whatis.com


Recommended