1 / 18

Working with XML Files in JavaScript: A Practical Guide

This chapter from "Beginning JavaScript" by Paul Wilton explores how to create and read XML files using JavaScript. It demonstrates the design tags for data input in XML as well as saving files. You will learn to declare and instantiate arrays for storing state names and capitals, and how to load XML data into these arrays using ActiveXObjects. The tutorial covers asynchronous behavior while loading XML and parsing state data effectively. By the end, you will have a solid understanding of handling XML files in JavaScript for building applications.

Télécharger la présentation

Working with XML Files in JavaScript: A Practical Guide

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Making and Reading from XML Files Chapter 14 of Beginning JavaScript (Paul Wilton)

  2. File/New/File

  3. Choose an XML File template and click Open

  4. Start of XML File

  5. Design tags and enter data.

  6. In the Data view

  7. Save file.

  8. Saving File

  9. Enter more data and save

  10. State Capital Quiz design

  11. Code for reading XML file for State data and initializing arrays

  12. Declare and instantiate arrays for state names and capitals var QUIZ_SIZE=5; var MyStateName = new Array(50); var MyStateCapital = new Array(50);

  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.

  14. Webopedia definition of ActiveX

  15. Webopedia definition of asynchronous

  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.

  17. Whatis definition of tree

  18. References • Beginning JavaScript, Paul Wilton • http://www.webopedia.com • http://www.whatis.com

More Related