?? display.php-n=xml.js&f=21-xml.js.htm
字號:
<html><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Keywords" name="Keywords"content="JavaScript, examples, JavaScript: The Definitive Guide"><title>Example xml.js from JavaScript: The Definitive Guide, Fifth Edition</title><style>body { font-family: arial, sans-serif}.lefttitle { font: bold x-small arial, sans-serif; margin: 5px; letter-spacing: .15em; text-transform:uppercase;}.leftcaption { font: italic x-small sans-serif; margin-bottom: 20px;}#banner { font-family:sans-serif; color:#333; font-size:x-large; font-weight:normal; padding:15px; padding-top: 3px; border-bottom:4px double #666;}#banner h1 { font-size: x-large; margin: 0px; padding: 0px;}#banner .description { font-size:large; margin-left: 2px;}pre { border: solid black 1px; background-color: #ddd; padding: 10px; margin: 0px;} </style></script></head><body><div id="banner"><h1>davidflanagan.com/javascript5</h1><span class="description">Example xml.js from<i>JavaScript: The Definitive Guide, Fifth Edition</i></span></div><div style="float: left; text-align: center; width:200px; margin-right:20px"><div class="lefttitle">Buy The Book</div><iframe src="../../rcm.amazon.com/e/cm-t=davidflanagancom&o=1&p=8&l=as1&asins=0596101996&fc1=000000&IS2=1<1=_blank&lc1=0000ff&bc1=000000&bg1=ffffff&f=ifr" tppabs="http://rcm.amazon.com/e/cm?t=davidflanagancom&o=1&p=8&l=as1&asins=0596101996&fc1=000000&IS2=1<1=_blank&lc1=0000ff&bc1=000000&bg1=ffffff&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe><div class="leftcaption">This example is posted here for the convenienceof my readers.</div><div class="lefttitle">Tip the Author</div><form action="https://www.paypal.com/cgi-bin/webscr" method="post"><input type="hidden" name="cmd" value="_s-xclick"><input type="image" src="../../www.paypal.com/en_US/i/btn/x-click-but21.gif" tppabs="https://www.paypal.com/en_US/i/btn/x-click-but21.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!"><img alt="" border="0" src="../../www.paypal.com/en_US/i/scr/pixel.gif" tppabs="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"><input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHXwYJKoZIhvcNAQcEoIIHUDCCB0wCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYADQkftkyvD87v5P15OJbQkb2IbGl9tDzTbZhLKhlUbLBQrkhCfnfuTM9jU8nFwjJPk0ydZnkIuM3mXoIlMrAWnTpmJPb2+JmRW4cdPmy7ET8SEayKGZVuh/z0HwP7XSWjYfN91QC8Dw0zvI575PV3Kggn/Hr0pbLO7W9TY4qX88TELMAkGBSsOAwIaBQAwgdwGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIxhIQ3LNTLmaAgbhxMeck3iCr8ij6ii9FtlNqTPr6xI86COz9DBGf1iPolPb9mEhyUUHOeRcZ0iZ4kP0hU+umGxnu8dF67WYwoD3Dpj4js/kzHA9/kO0GdPaonWkZBfR+i005EavkgNZ/atz7uFT2JOQ5kJEuRBXo0MmfVA1WoBus2fmgc3Hd9peXJ+eoUvEdmYV5EV6Rwn0xny1o3oY/jnDk8oh7fwxRYmVhWni9NZ2TyU27hR7j3TxijUbIGeGSzYTooIIDhzCCA4MwggLsoAMCAQICAQAwDQYJKoZIhvcNAQEFBQAwgY4xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLUGF5UGFsIEluYy4xEzARBgNVBAsUCmx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GUMIGOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxFDASBgNVBAoTC1BheVBhbCBJbmMuMRMwEQYDVQQLFApsaXZlX2NlcnRzMREwDwYDVQQDFAhsaXZlX2FwaTEcMBoGCSqGSIb3DQEJARYNcmVAcGF5cGFsLmNvbQIBADAJBgUrDgMCGgUAoF0wGAYJKoZIhvcNAQkDMQsGCSqGSIb3DQEHATAcBgkqhkiG9w0BCQUxDxcNMDYwODA3MDYwODQzWjAjBgkqhkiG9w0BCQQxFgQULV3HGvs2maVapQx6JqKdwMYPb9IwDQYJKoZIhvcNAQEBBQAEgYBkwPTf1ZY3TQkhiYlHNTzcz6cl9v+HZPpuKG07vXkfKZjRbQ+kTH7AnumG1l/+aVUXevEa6IF7156JW5P8pHyNwKK59diT/2CfkCgo2NpK2UfJ88O+83gFLxM8NLjZCra0lHb68MDpNPk1HSxfb2CJN5wkJyWsrAwHYyzlnT5hFg==-----END PKCS7-----"></form><div class="leftcaption">Found a helpful example, but don't own the book?</div><div class="lefttitle">Advertising</div><script type="text/javascript"><!--google_ad_client = "pub-5162555666633441";google_alternate_color = "ffffff";google_ad_width = 160;google_ad_height = 600;google_ad_format = "160x600_as";google_ad_type = "text_image";google_ad_channel ="";//--></script><script type="text/javascript" src="../../pagead2.googlesyndication.com/pagead/show_ads.js" tppabs="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div><div style="margin-left: 220px;"><div class="lefttitle" style="padding:0px; text-align: right"><a href="index.html" tppabs="http://www.davidflanagan.com/javascript5/index.html">Table of Examples</a></div><pre>/** * xml.js: utilities for creating, loading, parsing, serializing, * transforming and extracting data from XML documents. * * From the book JavaScript: The Definitive Guide, 5th Edition, * by David Flanagan. Copyright 2006 O'Reilly Media, Inc. (ISBN: 0596101996) */// Make sure we haven't already been loadedvar XML;if (XML && (typeof XML != "object" || XML.NAME)) throw new Error("Namespace 'XML' already exists");// Create our namespace, and specify some meta-informationXML = {};XML.NAME = "XML"; // The name of this namespaceXML.VERSION = 1.0; // The version of this namespace/** * 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; }};/** * Synchronously load the XML document at the specified URL and * return it as a Document object */XML.load = function(url) { // Create a new document the previously defined function var xmldoc = XML.newDocument(); xmldoc.async = false; // We want to load synchronously xmldoc.load(url); // Load and parse return xmldoc; // Return the document};/** * Asynchronously load and parse an XML document from the specified URL. * When the document is ready, pass it to the specified callback function. * This function returns immediately with no return value. */XML.loadAsync = function(url, callback) { var xmldoc = XML.newDocument(); // If we created the XML document using createDocument, use // onload to determine when it is loaded if (document.implementation && document.implementation.createDocument) { xmldoc.onload = function() { callback(xmldoc); }; } // Otherwise, use onreadystatechange as with XMLHttpRequest else { xmldoc.onreadystatechange = function() { if (xmldoc.readyState == 4) callback(xmldoc); }; } // Now go start the download and parsing xmldoc.load(url);};/** * Parse the XML document contained in the string argument and return * a Document object that represents it. */XML.parse = function(text) { if (typeof DOMParser != "undefined") { // Mozilla, Firefox, and related browsers return (new DOMParser()).parseFromString(text, "application/xml"); } else if (typeof ActiveXObject != "undefined") { // Internet Explorer. var doc = XML.newDocument(); // Create an empty document doc.loadXML(text); // Parse text into it return doc; // Return it } else { // As a last resort, try loading the document from a data: URL // This is supposed to work in Safari. Thanks to Manos Batsis and // his Sarissa library (sarissa.sourceforge.net) for this technique. var url = "data:text/xml;charset=utf-8," + encodeURIComponent(text); var request = new XMLHttpRequest(); request.open("GET", url, false); request.send(null); return request.responseXML; }};/** * 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 XML.Transformer class encapsulates an XSL stylesheet. * If the stylesheet parameter is a URL, we load it. * Otherwise, we assume it is an appropriate DOM Document */XML.Transformer = function(stylesheet) { // Load the stylesheet if necessary if (typeof stylesheet == "string") stylesheet = XML.load(stylesheet); this.stylesheet = stylesheet; // In Mozilla-based browsers, create an XSLTProcessor object and // tell it about the stylesheet. if (typeof XSLTProcessor != "undefined") { this.processor = new XSLTProcessor(); this.processor.importStylesheet(this.stylesheet); }};/** * This is the transform() method of the XML.Transformer class. * It transforms the specified xml node using the encapsulated stylesheet. * The results of the transformation are assumed to be HTML and are used to * replace the content of the specified element */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -