JavaScript
  Home arrow JavaScript arrow Page 2 - 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 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 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: 3 stars3 stars3 stars3 stars3 stars / 12
    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
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    JavaScript and XML - 21.1.1 Creating a New Document


    (Page 2 of 4 )

    You can create an empty (except for an optional root element) XML Document in Firefox and related browsers with the DOM Level 2 method document.implementation.createDocument() . You can accomplish a similar thing in IE with the ActiveX object named MSXML2.DOMDocument. Example 21-1 defines an XML.newDocument() utility function that hides the differences between these two approaches. An empty XML document isn’t useful by itself, but creating one is the first step of the document loading and parsing techniques that are shown in the examples that follow this one.

    Example 21-1. Creating an empty XML document

    /**
     
    * Create a new Document object. If no arguments are specified,
     * the document will be empty. If a root tag is specified, the document
     
    * will contain that single root tag. If the root tag has a namespace
     
    * prefix, the second argument must specify the URL that identifies the
     
    *namespace.
     * /
    XML.newDocument = function(rootTagName, namespaceURL) {
        if (!rootTagName) rootTagName = "";
        if (!namespaceURL) namespaceURL = "";

        if (document.implementation && document.implementation.createDocument) {
            // This is the W3C standard way to do it
            return document.implementation.createDocument(namespaceURL, 
                           
    rootTagName, null);
        }
        else { // This is the IE way to do it
            // Create an empty document as an ActiveX object
            // If there is no root element, this is all we have to do
            var doc = new ActiveXObject("MSXML2.DOMDocument");

            // If there is a root tag, initialize the document
           
    if (rootTagName) {
                // Look for a namespace prefix
                var prefix = "";
                var tagname = rootTagName;
                var p = rootTagName.indexOf(':');
                if (p != -1) {
                    prefix = rootTagName.substring(0, p);
                    tagname = rootTagName.substring(p+1);
                }

                // If we have a namespace, we must have a namespace prefix
                // If we don't have a namespace, we discard any prefix
                if (namespaceURL) {
                   
    if (!prefix) prefix = "a0"; // What Firefox uses
                }
                else prefix = "";

                // Create the root element (with optional namespace) as a
                // string of text
                var text = "<" + (prefix?(prefix+":"):"") + tagname +
                   
    (namespaceURL
                     ?(" xmlns:" + prefix + '="' + namespaceURL +'"')
                     :"") +
                   
    "/>";
                // And parse that text into the empty document
                doc.loadXML(text);
            }
            return doc;
        }
    };

    More JavaScript Articles
    More By O'Reilly Media


       · 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

    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - 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







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