Binary data cdata
XML is a well-supported Internet standard for encoding structured data in a way that can be easily decoded by practically any programming language and even read or written by humans using standard text editors. Many applications, especially modern standards-compliant Web browsers, can deal directly with XML data. As a text-based standard, XML is well-suited for exchanging data between client and server systems.
Much data is already text-based file paths, descriptions, addresses, names, and so onand things like integers, floating-point numbers, and dates can be easily converted to and from string representations.
This expands the data and makes it extremely hard for humans to read, not to mention the annoyance of translating markup if you write the XML manually in a text editor. A better solution might be to put the data directly into your XML document. CDATA character data sections binary data cdata treated as a block of data by the parser, allowing you to include any character in the binary data cdata stream. Listing 1 shows a simple paragraph sample with some emphasized text.
It becomes a bit of a nightmare when you want to show the markup see Listing 2. Anything between those bits of markup will binary data cdata through the XML parser untouched. You'll often see something like Listing 4. This isn't generally going to cause trouble unless you're very, very unlucky, but it can certainly cause parser errors that lead to confusing and hard-to-debug rendering errors.
Also, the dash dash -- binary data cdata can be seen as the unexpected start or end of an XHTML comment block. Clearly the CDATA section is useful, but like all good things, it has a couple of limitations for you to keep in mind. You'll either lose their contents the CDATA section has vanished from the normal Binary data cdata or have the contents rendered as text with some stray markup characters showing up.
To see this effect, look at a page binary data cdata shows the sample paragraph, the sample paragraph with the markup visible using entitiesand an attempt to show the sample paragraph with the markup visible binary data cdata CDATA. View a text-only version of Figure 1. View image at full size. WebKit-based binary data cdata such as Safari and Chrome render it with spurious markup characters see Figure 2. View a text-only version of Figure 2. View a text-only version of Figure 3. If they didn't, the browser's XML parser would be considered "non-conforming" and people would mock it mercilessly before marking it as horrifically broken for Ajax.
If the XML parser reads this sequence, it's the end of binary data cdata CDATA section and binary data cdata might end up getting a parser error when it hits the real section end. Luckily, this situation doesn't come up very often. Even though the contents binary data cdata the CDATA section pass through your parser untouched, they still need to be valid XML data characters, as binary data cdata by the document's character encoding. Using something like UTF-8 lets you use a huge range of characters for the data, but it's not 8-bit clean.
Any of the so-called binary data cdata characters those with a hex value below 0x20, the space character can cause your parser to stop with an invalid token error. If you serve the XML files through a Web service, make sure that your client applications can deal with potentially large data transfers without timing out or blocking their user interface as the data trickles in over a 3G connection.
The reverse is also true; make sure your server can accept large up-stream transfers from clients sending XML data. Binary data cdata large blocks of data from the browser like this is error-prone for example, what if the user cancels the transfer because they think it has crashed? And again, depending on what you're doing, you need to keep in mind that many people are using mobile platforms and binary data cdata might also be stuck on dial-up connections still!
Even if you didn't design it that way, someone will try using it over a dial-up VPN connection on their iPhone, and they'll complain about your application's speed instead of their poor life choices! When you do need to include some binary data in an XML document, you'll binary data cdata to make sure it won't trip up binary data cdata XML parser.
If the data happens to be text, you can dump it into a CDATA section and be done with it, but true binary data needs to be encoded in a safe and recoverable manner. Luckily the MIME standards define a safe encoding scheme that's well-supported, base Typically you'd want to indicate the encoding and original file name in your XML, as in Listing 9. In a machine-generated XML document, you can leave out the white space, and run the entire baseencoded file together without newline characters.
The best way to deal with binary data in XML is to avoid it entirely. As you've seen in HTML, referring to an external file in a standardized way works well. This is a great option when you have some binary data cdata for the client application to get at the external file. By not including the binary data directly in the XML, you avoid potentially wasteful text encodings and make it possible to implement other enhancements, such as the image caching most people love in their Web browsers.
Using Binary data cdata to embed marked-up data directly in your XML documents keeps you from having to encode the data, but you need to be careful and consider the effect of potentially large data transfers on your client and server applications. When you need to store binary data in an XML binary data cdata, you can use a text encoding such as the standard MIME base64 encoding, although it's probably a better idea to reference an external file. Sign in or register to add and subscribe to comments.
Chris Herborth Published on January 12, Frequently used acronyms Ajax: Application programming interface CSS: Local area network MIME: Comments Sign in or register to add and subscribe to comments.
Subscribe me to comment notifications.