Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Parsing XML using AJAX

From: JavaScript and AJAX

Video: Parsing XML using AJAX

AJAX was originally designed to read data stored in a format called XML, or Extensible Markup Language. It's very much like HTML, but you get to make your own tags. So let's take a look at an example of what it takes like to read data in this format. We have a new file in our folder called data.xml. It's a file with some data in the XML format. If you know a little HTML this should be super easy to read. You can open an XML file with some browsers to see the structure a little bit better. So we have a speakers object, and inside the speaker objects we have a series of speaker objects.

Parsing XML using AJAX

AJAX was originally designed to read data stored in a format called XML, or Extensible Markup Language. It's very much like HTML, but you get to make your own tags. So let's take a look at an example of what it takes like to read data in this format. We have a new file in our folder called data.xml. It's a file with some data in the XML format. If you know a little HTML this should be super easy to read. You can open an XML file with some browsers to see the structure a little bit better. So we have a speakers object, and inside the speaker objects we have a series of speaker objects.

Each one of the speaker objects has different parts to it, so for example we have the tag, the tag--which is the same as the tag but without spaces or special characters--as well as the tag and a that has some information about the speaker. Reading this into JavaScript is super simple. As a matter of fact, the XHR object comes with a special attribute called responseXML. It's sort of like the responseText property, except that it converts the data into an object that you can manipulate through JavaScript.

So, before we do anything else, let's go back into our HTML file and change this so we're back to a div with an id of update. So, I am going to go ahead and save this, and we are going to switch to the script.js file. And now we need to change the request so that it doesn't request the data.txt document, but instead uses our XML document. I am also going to remove the lines of code that are looping through all of my elements. So now that we are linking to the XML document instead of the text document let's just take a look at the responseXML property of our request.

So let me save that and switch over to my browser, and let me hit the Back button to go back into my file, and then I'll refresh this. So, let's go into our console and take a look at what we get back. So, you see that what we get back is essentially a document with some data inside it that's pretty much like what we saw in the XML file. The XML object is really easy to go through. We can use the getElementsByTagName with an HTML or an XML document. So if we wanted to get to, say, the name of the second user, you can do something like this.

So let me go ahead and save that, and then I'll switch over to my browser and we will hit Refresh. So what we get back is the entire tag of that XML node. So every element inside an XML structure is considered a node. Elements inside other elements, including the text inside an element, is also considered a node. So, to get to the text of this node we could use this. So let me save that and switch back to my browser and hit Refresh. You can see that we are getting the name. It still has quotes which is not exactly correct, but at least we are not getting the tag anymore.

So there is actually a shortcut for the first element in any array, and it's called firstChild. So if we do this, it's the same as writing child nodes zero. Refresh, we get the same results. Back over here, so if you look at what we get, it's the value in quotes, and it's not exactly the text of the node. To get that we would have to use the nodeValue property like this. So I am going to save that, switch over to my browser, refresh, and now we get just the text without the quotes. So, let's see what it will take to make a list of our speakers.

We'll have to cycle through the XML file using a for loop. When the request is ready we'll create a variable that looks for the elements with a tag of name. Then we are going to create an output variable with an unordered list. Inside this, we are going to create a loop. And here we'll go ahead and add the value of each result as list items.

Now we will send the results of all this to our div with a value of update. So let me save this and preview this in my browser, and you can see the list of all our names. So although you can read data in any format besides XML, the AJAX API was designed with an easy-to-use responseXML property that makes it a trivial matter to read and parse XML files.

Show transcript

This video is part of

Image for JavaScript and AJAX
JavaScript and AJAX

21 video lessons · 19531 viewers

Ray Villalobos
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript and AJAX.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.