Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb
"Accelerated DOM Scripting with Ajax, APIs, and Libraries"
firstChild.data;
With the first
element, everything looks the way it should, and the entire string
is embedded. But wait a second, from the second XML node example, only "This is some"
appears on the page. That??™s because the tags are treated like another node, as
shown in Figure 5-8.
Figure 5-8. What the XML structure looks like
There are a couple of ways to get around having to parse through all the nodes: simply
encode any HTML or embed the HTML in a CDATA node.
Encoding HTML
XML has five characters that can be encoded: &, <, >, ', and ". These characters can be encoded
with their numerical character references or their predefined entities, as shown in Table 5-2.
CHAPTER 5 n AJAX AND DATA EXCHANGE 110
Table 5-2. Available Predefined Entities
Character Numerical Character Reference Predefined Entity
& & &
< < <
> > >
' ' '
" " "
Knowing this, you can re-encode the example from before:
This is some <strong>content</strong>
I want embedded on the page
If you are familiar with encoding characters in HTML, you can easily fall into another
trap: using HTML entities in XML. This, too, will get caught by the XML error handling and fail.
This page copyright by Smith & Smith
This page copyright by Smith & Smith
© Smith & Smith
The first example should be obvious because the ampersand hasn??™t been encoded.
Pages:
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175