Chapter 4. Sending and Receiving Data

In This Chapter

  • Summarizing XML

  • Generating an XML response

  • Traversing XML with JavaScript

  • Introducing JSON

AJAX can be used to send information to the client from the server. Elsewhere in Book VII, I concentrate on using complete XHTML code that can be integrated directly into the page. There's another powerful way to work with AJAX. You can pass data around in a specialized format and let the JavaScript/jQuery code manipulate the data on the client. The data is usually packed in XML format, but JSON is an increasingly popular technique. Both systems are introduced in this chapter.

Working with XML

eXtensible Markup Language (XML) is an extremely useful tool for sending and receiving data. XML data is easy to read and understand, even by humans. It's not too difficult to write programs that can read XML data, and the structure of the data is preserved by the tags. When you create your own XML format, you define custom tags to describe the data as follows:

<person>
    <name>
        <first>John</first>
        <middle>L</middle>
        <last>Doe</last>
    </name>
    <age>25</age>
    <gender>Male</gender>
</person>

Tip

XML is nothing new if you've read any other part of this book. XHTML is simply a form of HTML that also follows XML rules. The new idea is this: Rather than having to conform to somebody else's list of tags, you can invent your own and transmit information using your custom XML syntax.

These tags can be parsed with JavaScript and generated with PHP fairly easily.

Every XML element ...

Get HTML, XHTML, and CSS All-In-One Desk Reference For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.