Making and Reading from XML Files Chapter 14


















- Slides: 18

Making and Reading from XML Files Chapter 14 of Beginning Java. Script (Paul Wilton) CSD 340 (Blum) 1

File/New/File CSD 340 (Blum) 2

Choose an XML File template and click Open CSD 340 (Blum) 3

Start of XML File CSD 340 (Blum) 4

Design tags and enter data. CSD 340 (Blum) 5

In the Data view CSD 340 (Blum) 6

Save file. CSD 340 (Blum) 7

Saving File CSD 340 (Blum) 8

Enter more data and save CSD 340 (Blum) 9

State Capital Quiz design CSD 340 (Blum) 10

Code for reading XML file for State data and initializing arrays CSD 340 (Blum) 11

Declare and instantiate arrays for state names and capitals var QUIZ_SIZE=5; var My. State. Name = new Array(50); var My. State. Capital = new Array(50); CSD 340 (Blum) 12

//loads data from xml file into the arrays (IE only) function Load. State. Array() { var xml. Doc = new Active. XObject("Microsoft. XMLDOM"); xml. Doc. async = false; xml. Doc. load("States. xml"); Declare and instantiate and Active. X 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. CSD 340 (Blum) 13

Webopedia definition of Active. X CSD 340 (Blum) 14

Webopedia definition of asynchronous CSD 340 (Blum) 15

for(i=0; i< xml. Doc. get. Elements. By. Tag. Name("state"). length; i++) { My. State. Name[i] = xml. Doc. get. Elements. By. Tag. Name("state")[i]. get. Elements. By. Tag. Nam e("state. Name")[0]. first. Child. node. Value; My. State. Capital[i] = xml. Doc. get. Elements. By. Tag. Name("state")[i]. get. Elements. By. Tag. Nam e("state. Capital")[0]. first. Child. node. Value; } //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. CSD 340 (Blum) 16

Whatis definition of tree CSD 340 (Blum) 17

References • Beginning Java. Script, Paul Wilton • http: //www. webopedia. com • http: //www. whatis. com CSD 340 (Blum) 18