19.12. Removing Extra Whitespace from XML Objects

Problem

You want to remove whitespace nodes from your XML object because they are interfering with your ability to programmatically extract the values you want.

Solution

Set the ignoreWhite property to true before loading external data into an XML object or before parsing any string into an XML object, or use a custom removeWhitespace( ) method to remove whitespace nodes after loading data or for legacy purposes.

Discussion

XML documents are often written with extra whitespace characters for the purposes of formatting so that it is easier for humans to read. Most of the time, each element appears on its own line (meaning that newline, carriage return, or form feed characters have been added), and child nodes are sometimes indented using tabs or spaces. In what follows, you can see the same XML data formatted in two ways. The first is the data formatted without the whitespace characters, and the second is the same data with formatting. Which is easier to read?

<book><title>ActionScript Cookbook</title><authors><author name="Joey Lott" /></authors></book>

or:

<book>
  <title>ActionScript Cookbook</title>
  <authors>
    <author name="Joey Lott" />
  </authors>
</book>

While the formatted XML data may be easier for you, as a human, to read, the unformatted XML data is better suited for Flash. Flash counts all of the whitespaces as their own text nodes, which means that Flash parses every newline or tab character into its own node. The result is that ...

Get Actionscript Cookbook 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.