JavaScript
  Home arrow JavaScript arrow Page 4 - JavaScript and XML
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JavaScript and XML
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2007-08-08

    Table of Contents:
  • JavaScript and XML
  • 21.1.1 Creating a New Document
  • 21.1.2 Loading a Document from the Network
  • 21.1.4 XML Documents from Data Islands

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    JavaScript and XML - 21.1.4 XML Documents from Data Islands


    (Page 4 of 4 )

     

    Microsoft has extended HTML with an <xml> tag that creates an XML data island within the surrounding “sea” of HTML markup. When IE encounters this <xml> tag, it treats its contents as a separate XML document, which you can retrieve using document.getElementById() or other HTML DOM methods. If the <xml> tag has a src attribute, the XML document is loaded from the URL specified by that attribute instead of being parsed from the content of the <xml> tag.

    If a web application requires XML data, and the data is known when the application is first loaded, there is an advantage to including that data directly within the HTML page: the data is already available, and the web application does not have to estab lish another network connection to download the data. XML data islands can be a useful way to accomplish this. It is possible to approximate IE data islands in other browsers using code like that shown in Example 21-5.

    Example 21-5. Getting an XML document from a data island

    /**
     
    * Return a Document object that holds the contents of the <xml> tag
     
    * with the specified id. If the <xml> tag has a src attribute, an XML
     
    * document is loaded from that URL and returned instead.
      *
     
    * Since data islands are often looked up more than once, this function caches
     
    * the documents it returns.
     */
    XML.getDataIsland = function(id) {
        var doc;

        // Check the cache first
        doc = XML.getDataIsland.cache[id];
        if (doc) return doc;

        // Look up the specified element
        doc = document.getElementById(id);

        // If there is a "src" attribute, fetch the Document from that URL
        var url = doc.getAttribute('src');
        if (url) {
           
    doc = XML.load(url);
        }
        // Otherwise, if there was no src attribute, the content of the <xml>
        // tag is the document we want to return. In Internet Explorer, doc is
        // already the document object we want. In other browsers, doc refers to
        // an HTML element, and we've got to copy the content of that element
        // into a new document object
        else if (!doc.documentElement) {// If this is not already a document...

            // First, find the document element within the <xml> tag. This is
            // the first child of the <xml> tag that is an element, rather
            // than text, comment, or processing instruction
            var docelt = doc.firstChild;
            while(docelt != null) {
                if (docelt.nodeType == 1 /*Node.ELEMENT_NODE*/) break;
                docelt = docelt.nextSibling;
            }

            // Create an empty document
            doc = XML.newDocument();

            // If the <xml> node had some content, import it into the new document
            if (docelt) doc.appendChild(doc.importNode(docelt, true));
        }

        // Now cache and return the document
        XML.getDataIsland.cache[id] = doc;
        return doc;
    };
    XML.getDataIsland.cache = {}; // Initialize the cache

    This code does not perfectly simulate XML data islands in non-IE browsers. The HTML standard requires browsers to parse (but ignore) tags such as <xml> that they don’t know about. This means that browsers don’t discard XML data within an <xml> tag. It also means that any text within the data island is displayed by default. An easy way to prevent this is with the following CSS stylesheet:

      <style type="text/css">xml { display: none; }</style>

    Another incompatibility is that non-IE browsers treat the content of XML data islands as HTML rather than XML content. If you use the code in Example 21-5 in Firefox, for example, and then serialize the resulting document (you’ll see how to do this later in the chapter), you’ll find that the tag names are all converted to upper case because Firefox thinks they are HTML tags. In some cases, this may be problematic; in many other cases, it is not. Finally, notice that XML namespaces break if the browser treats the XML tags as HTML tags. This means that inline XML data islands are not suitable for things like XSL stylesheets (XSL is covered in more detail later in this chapter) because those stylesheets always use namespaces.

    If you want the network benefits of including XML data directly in an HTML page, but don’t want the browser incompatibilities that come with using XML data islands and the <xml> tag, consider encoding your XML document text as a JavaScript string and then parsing the document using code like that shown in Example 21-4.

    Please check back tomorrow for the continuation of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "JavaScript: The Definitive Guide, Fifth...
     

    Buy this book now. This article is excerpted from chapter 21 of the book JavaScript: The Definitive Guide, Fifth Edition, written by David Flanagan (O'Reilly; ISBN: 0596101996). Check it out today at your favorite bookstore. Buy this book now.

    JAVASCRIPT ARTICLES

    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps
    - Adding Zoom Controls to Yahoo! Maps
    - Working with Yahoo! Maps
    - Building Image Zooming Controls with the DOM...
    - Working with Multiple Graphics for a Zoom Ap...

    Iron Speed

    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway