惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
Google DeepMind News
Google DeepMind News
Hugging Face - Blog
Hugging Face - Blog
D
Docker
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
J
Java Code Geeks
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Simon Willison's Weblog
Simon Willison's Weblog
S
Security Affairs
NISL@THU
NISL@THU
T
Tor Project blog
A
About on SuperTechFans
宝玉的分享
宝玉的分享
腾讯CDC
S
Schneier on Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
P
Privacy International News Feed
雷峰网
雷峰网
C
Cyber Attacks, Cyber Crime and Cyber Security
Vercel News
Vercel News
Cisco Talos Blog
Cisco Talos Blog
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
L
LINUX DO - 热门话题
Microsoft Security Blog
Microsoft Security Blog
Latest news
Latest news
C
Check Point Blog
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
云风的 BLOG
云风的 BLOG
SecWiki News
SecWiki News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
爱范儿
爱范儿
月光博客
月光博客
V
Vulnerabilities – Threatpost
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs

博客园 - 龍的傳人

再谈代码生成器,拥有自己最适合的代码生成器 javascript+css+xml 全选树(再续) 设表格细钱 javascript+css+xml 全选树(续) javascript+css+xml 全选树 白话CMMI 如何做好需求收集[来之《程序员》第2期] javascript、CSS、XML动太生成树菜单 - 龍的傳人 - 博客园 可选择也可填写的下拉框(用鼠标\键盘的上下键选择) - 龍的傳人 - 博客园 兼容firefox和IE的两级联动下拉菜单的javascript代码 HTML 图片分析 firefox与IE对javascript和CSS的区别(转) IE和Firefox在JavaScript方面的兼容性(转) javascript在中ie与firefox的区别与解决方案(转) 针对Firefox兼容性,要注意的一些问题 (转) sql2000和文本文件的写入和读取(转) [转]JS代码格式化工具(附源代码) ASP.NET2.0调用MySql的存储过程 Remote建立分析
JavaScript - Import XML Document
龍的傳人 · 2008-01-10 · via 博客园 - 龍的傳人

function importXML()
{
    
if (document.implementation && document.implementation.createDocument)
    
{
        xmlDoc 
= document.implementation.createDocument(""""null);
        xmlDoc.onload 
= createTable;
    }

    
else if (window.ActiveXObject)
    
{
        xmlDoc 
= new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.onreadystatechange 
= function () {
            
if (xmlDoc.readyState == 4) createTable()
        }
;
     }

    
else
    
{
        alert(
'Your browser can\'t handle this script');
        return;
    }
    xmlDoc.load("emperors.xml");
}

function createTable()
{
    var x = xmlDoc.getElementsByTagName(
'emperor');
    var newEl = document.createElement(
'TABLE');
    newEl.setAttribute(
'cellPadding',5);
    var tmp = document.createElement(
'TBODY');
    newEl.appendChild(tmp);
    var row = document.createElement(
'TR');
    for (j=0;j<x[0].childNodes.length;j++)
    {
        if (x[0].childNodes[j].nodeType != 1) continue;
        var container = document.createElement(
'TH');
        var theData = document.createTextNode(x[0].childNodes[j].nodeName);
        container.appendChild(theData);
        row.appendChild(container);
    }
    tmp.appendChild(row);
    for (i=0;i<x.length;i++)
    {
        var row = document.createElement(
'TR');
        for (j=0;j<x[i].childNodes.length;j++)
        {
            if (x[i].childNodes[j].nodeType != 1) continue;
            var container = document.createElement(
'TD');
            var theData = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
            container.appendChild(theData);
            row.appendChild(container);
        }
        tmp.appendChild(row);
    }
    document.getElementById(
'writeroot').appendChild(newEl);
}

Importing the XML
First of all I import the XML document and make it accessible through the object xmlDoc. When the document has finished loading, I want the script createTable() that construes the table to be executed immediately. Of course, the coding for all this is browser specific.

Clicking the link activates the function importXML.

function importXML()
{

Mozilla
Netscape imports an XML document through the method document.implementation.createDocument(). First check if document.implementation is supported, then check if document.implementation.createDocument() is supported. Explorer 5 on Mac also supports document.implementation, but not the createDocument method, so it shouldn
't execute this script.

    
if (document.implementation && document.implementation.createDocument)
    
{

Then create the document and give it an onLoad event handler: as soon as the document has been loaded the script createTable() is executed, creating the table:

        xmlDoc 
= document.implementation.createDocument(""""null);
        xmlDoc.onload 
= init;
    }


Explorer
Explorer on Windows doesn
't support document.implementation . Instead, you must create an Active X Object that will contain the XML document. So we see if the browser can create ActiveXObjects:

    else if (window.ActiveXObject)
    {

If it does, we can proceed by creating the object

        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

Unfortunately there
's no onLoad event for this object. To see if it's ready we should use the MS proprietary ReadyStateChange event. I don't quite understand all this myself, but it works. When the onReadyStateChange event handler fires, the readyState has a value between 1 and 44 means that all data has been received (= onLoad). So if it's 4, start creating the table.

        xmlDoc.onreadystatechange = function () {
            if (xmlDoc.readyState == 4) createTable()
        };
    }

Other browsers
If the browser supports neither way, give an alert and end everything:

    else
    {
        alert(
'Your browser can\'t handle this script');
        
return;
    }


/**
 * 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;
  }

}
;