Parsing and transforming XML in AJAX programs
Introduction: Any programming problem can be solved by a variety of correct methods. This series examines four ways to create a asynchronous JavaScript + XML (Ajax) Weather Report Panel (weather Badge), a small reusable widget that can be easily embedded into any Web page. This is the first article that introduces some of the basics and examines the first approach-traversing the DOM tree.
The Greek philosopher Aristotle once wrote: "There are many ways to fail ... but there is only one way to success." "Sadly, Aristotle is not a computer programmer," said the man.
Although Aristotle's first assertion conformed to the characteristic of programming-"There are many paths to failure"-his second guess has no basis.
This series of articles employs four methods for the same problem. Each method is proven to be correct-each has its own pros and cons. The problems they are trying to solve are not complicated, and the solution is not complicated. However, these methods involve a large number of "trade-offs" that can be included in a very simple solution.
Problem: Creating a reusable Ajax Weather dashboard
To define the issues that need to be resolved, I have listed the following problem descriptions:
Build an Ajax library to read the latest observational data obtained from National Weather Service, and then extract part of the data and convert it to HTML format to create a weather report panel.
Many web sites like to display local weather conditions on a Web page. To do this, they need access to the latest meteorological information. So how do you get the data?
In the United States, national Weather Service (NWS) provides a great deal of weather information. These include current weather observations from hundreds of cities in the United States. This data can be used in either RSS or XML format.
In Ajax, the letter X represents XML, so NWS data seems to fit well with Ajax methods.
Four Possible solutions
This series of articles examines four different methods for building an Ajax weather panel-A small information box containing meteorological information for any city or town monitored by NWS. Its design goal is
of simplicity
Easy to reuse
I will use these four methods to demonstrate the inherent "trade-offs" of each approach. There is no absolute right or wrong in any of these ways.
Essentially, the implementation of each method is very different, as described in Table 1.