Chapter 9. XML Requests and Responses: More Than Words Can Say

image with no caption

How will you describe yourself in 10 years? How about 20?

Sometimes you need data that can change with your needs... or the needs of your customers. Data you’re using now might need to change in a few hours, or a few days, or a few months. With XML, the extensible markup language, your data can describe itself. That means your scripts won’t be filled with ifs, elses, and switches. Instead, you can use the descriptions that XML provides about itself to figure out how to use the data the XML contains. The result: more flexibility and easier data handling.

Note

As a special bonus, we’re bringing back the DOM in this chapter... keep an eye out!

Rob’s Rock and Roll Memorabilia has hit the big time. Since going online with the site you built for Rob, he’s selling collectible gear to rich customers around the world.

In fact, Rob’s gotten lots of good feedback on the site, and he’s making some improvements. He wants to include a price for each item, in addition to the description, and he also wants to be able to include a list of related URLs so customers can find out more about each item.

image with no caption

So far, all the server-side programs we’ve worked with have sent back a single piece of data, like -1 or “okay.” But now Mike wants to send back several pieces of data at one time:

image with no caption

What would you do?

There are lots of ways to handle getting more than one value back from the server, and this time, the choice is up to you. What do you think?

image with no caption
image with no caption

Jill: I don’t know, Joe. That’s a lot of formatting for the server-side guys to keep up with.

Joe: But they’ve got all the data about the item, right?

Jill: Well, sure... but server-side programmers don’t really like to mess around with XHTML. That’s the whole reason a lot of these folks move over to the server-side in the first place... no XHTML.

Joe: But the CSS doesn’t change that much, so the XHTML won’t change that often.

Jill: Oh, the XHTML will change sometimes?

Joe: Well, sure, maybe... but not very often. Only if we need to add a tag, or maybe an ID for CSS...

Jill: Oh, no. You’re not going to be able to get server-side guys to write XHTML, and then change it all the time on top of that.

Joe: Hmmm. This is sounding harder than I thought. I thought innerHTML was going to be really simple...

From a client-side point of view, innerHTML is pretty simple to use. You just get an XHTML response from the server, and drop it into a web page with an element’s innerHTML property.

function displayDetails() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      detailDiv = document.getElementById("description");
      detailDiv.innerHTML = request.responseText;
    }
  }
}

The problem is that the server has to do a lot of extra work. Not only does the server have to get the right information for your app’s request, it has to format that response in a way that’s specific to your application. In fact, that format is specific to one individual page on your site!

image with no caption
image with no caption

Frank: I’m not sure, Jim. Something’s bugging me about this CSV thing.

Jim: What, just because I’m already almost done?

Frank: No, seriously. It just seems so ... I don’t know. Inflexible?

Jim: What do you mean? Here, look at my code to take the server’s response in my callback, and update the item detail for the page. It’s a little long, but it’s all pretty basic stuff:

image with no caption
image with no caption
image with no caption

Jill: Sure, XML is easy. The server-side guys shouldn’t have any problem with that at all. It’s certainly a lot better than XHTML...

Frank: Yeah, I heard Joe was working on that. The server guys couldn’t get him an XHTML response?

Jill: Well, they could’ve, but nobody wanted to. XHTML is a mess to work with on the server, and it changes all the time.

Frank: You know XHTML is just a flavor of XML, right?

Jill: Sure, but lots of people and apps can use XML. Dealing with a certain <div> with this id, or only using <p>'s and not <br />'s... that’s pretty fragile.

Frank: No kidding. Well, I’ve got to rewrite my callback, but let me know when the XML response is ready, okay?

Since XHTML is really a particular implementation of XML, it makes sense that you can use the DOM to work with XML, too. In fact, the DOM is really designed to work with XML from the ground up.

Even better, the request object you’ve been using to talk to the server has a property that returns a DOM tree version of the server’s response. That property is called responseXML, and you use it like this:

image with no caption
image with no caption

Rob wants data that changes depending on the request.

If you ask the server for details about a guitar, you’ll get a manufacturer and year. Clothing? A manufacturer, sure, but also a size. And for bands, you’ll get a band name, and possibly the name of the individual in the band that the item belonged to or is associated with.

How would you handle a changing response from the server? And who’s better equipped to handle this new requirement? Frank, with his XML, or Jim, with his CSV?

image with no caption

Frank: Yeah, I’ve got a lot of changes to make, too. But I was thinking... Jim, how are you going to handle a changing response with your CSV?

Jim: Well, I was thinking about that...

Joe: Hey, guys, I had an idea. You know I’ve been doing some research—

Jim: So I think what I can do is assume that every other value is a category, like “Description” or “Price.” And the values after each category are the actual category values, like the textual description, or 399.99, or whatever.

Frank: Hmmm. Sounds a little hairy.

Jim: It’s not too bad. Except for cases where there’s more than one value, like for those URLs? Then I think I have to check for maybe a special character before each category to indicate that it’s a multi-value category.

Joe: Listen, guys, I wanted to show you—

Frank: Wow, Jim, that’s nasty. Sounds like this latest change from Rob is really going to be a pain.

Jim: Yeah, it kinda is. But what else can I do?

image with no caption

The thing that’s cool about XML is that you can create your own vocabulary. XHTML is an XML vocabulary that’s specific to displaying things on the web. But suppose we needed a vocabulary for describing items, like at Rob’s online store.

But the format can’t be locked into elements like <price> or <resources> because we want each item to define its own categories. We might use something like this:

image with no caption
image with no caption
image with no caption