⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dom_parser.asp@output=print

📁 W3Schools tutorial..web designing
💻 ASP@OUTPUT=PRINT
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>XML DOM Parser</title>
 
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />

<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

</head>
<body>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>


<h1>Parsing the XML DOM</h1>

<a href="dom_nodetree.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_loadxmldoc.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>

<hr />
<p class="intro">
Most browsers have a build-in XML parser to read and manipulate XML.</p>
<p class="intro">
The parser converts XML into a JavaScript accessible object. </p>

<hr />

<h2>Examples</h2>

<p>W3Schools examples are browser and platform independent. These examples work in 
all modern browsers.</p>

<p><a target="_blank" href="tryit.asp@filename=try_dom_parser">Load and parse an XML file</a></p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_parser2">Load and parse an XML 
string</a></p>

<hr />

<h2>Parsing XML</h2>

<p>
All modern browsers have a build-in XML parser that can be used to read and manipulate XML.</p>
<p>The parser reads XML into memory and converts it into an XML DOM object that 
can be accesses with JavaScript. </p>
<p>There are some differences between Microsoft's XML parser and the parsers 
used in other browsers. The Microsoft parser supports loading of both XML files 
and XML strings (text), while other browsers use separate parsers. However, all parsers contain functions to traverse XML trees, access, 
insert, and delete nodes. </p>
<p>In this tutorial we will show you how to create scripts that will work in both Internet Explorer and 
other browsers.</p>

<hr />
<h2>Loading XML with Microsoft's XML Parser</h2>
<p>
Microsoft's XML parser is built into Internet
Explorer 5 and higher.</p>

<p>The following JavaScript fragment loads an XML document (&quot;<a target="_blank" href="books.xml">books.xml</a>&quot;) into 
the parser:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table16">
<tr><td>
<pre>xmlDoc=new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
xmlDoc.async=&quot;false&quot;;
xmlDoc.load(&quot;books.xml&quot;);</pre>
</td></tr>
</table>

<p>
<b>Code explained:</b></p>
<ul>
	<li>The first line creates an empty Microsoft XML 
document object. </li>
	<li>The second
line turns off asynchronized loading, to make sure that the parser will not 
continue execution of the script before the document is fully loaded.</li>
	<li>The third 
line tells the parser to load an XML document called &quot;books.xml&quot;.</li>
</ul>

<p>The following JavaScript fragment loads a string called txt into 
the parser:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table17">
<tr><td>
<pre>xmlDoc=new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
xmlDoc.async=&quot;false&quot;;
xmlDoc.loadXML(txt);</pre>
</td></tr>
</table>
<p><b>Note:</b> The <b>loadXML()</b> method is used for loading strings (text), <b>load()</b> is used for loading 
files.</p>
<hr />
<h2>XML Parser in Firefox and Other Browsers</h2>

<p>The following JavaScript fragment loads an XML document (&quot;<a target="_blank" href="books.xml">books.xml</a>&quot;) into 
the parser:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table18">
<tr><td>
<pre>xmlDoc=document.implementation.createDocument(&quot;&quot;,&quot;&quot;,null);
xmlDoc.async=&quot;false&quot;;
xmlDoc.load(&quot;books.xml&quot;);</pre>
</td></tr>
</table>
<p>
<b>Code explained:</b></p>
<ul>
	<li>The first line creates an empty XML 
document object.</li>
	<li>The second
line turns off asynchronized loading, to make sure that the parser will not 
continue execution of the script before the document is fully loaded.</li>
	<li>The third 
line tells the parser to load an XML document called &quot;books.xml&quot;.</li>
</ul>

<p>The following JavaScript fragment loads a string called txt into 
the parser:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table19">
<tr><td>
<pre>parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,&quot;text/xml&quot;);</pre>
</td></tr>
</table>
<p>
<b>Code explained:</b></p>
<ul>
	<li>The first line creates an empty XML 
document object.</li>
	<li>The second 
line tells the parser to load a string called txt.</li>
</ul>

<p><b>Note:</b> Internet Explorer uses the <b>loadXML()</b> method to parse an XML 
string, while other browsers uses the <b>DOMParser</b> object.</p>


<hr />

<h2>Parsing an XML File - A Cross browser Example</h2>

<p>The following example loads an XML document (&quot;books.xml&quot;) into the XML parser:</p>

<table width="100%" border="1" class="ex" cellspacing="0" id="table21">
<tr><td>
<pre>&lt;html&gt;
&lt;body&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    xmlDoc=document.implementation.createDocument(&quot;&quot;,&quot;&quot;,null);
    }
  catch(e) {alert(e.message)}
  }
try 
  {
  xmlDoc.async=false;
  xmlDoc.load(&quot;books.xml&quot;);
  document.write(&quot;xmlDoc is loaded, ready for use&quot;);
  }
catch(e) {alert(e.message)}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</td></tr>
</table>

<p><a target="_blank" href="tryit.asp@filename=try_dom_parser">Try it yourself</a>
</p>


<hr />
<h2>Error: Access Across Domains </h2>
<p>For security reasons, modern browsers does not allow access across domains.</p>
<p>This means, that both the web page and the XML file it tries to load, must be 
located on the same server.</p>
<p>The examples on W3Schools all open XML files located on the W3Schools domain.</p>
<p>If you want to use the example above on one of your web pages, the XML files 
you load must be located on your own server. Otherwise the xmlDoc.load() method, 
will generate the error &quot;Access is denied&quot;.</p>


<hr />

<h2>Parsing an XML String - A Cross browser Example</h2>
<p>The following code loads and parses an XML string:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table24">
<tr><td>
<pre>&lt;html&gt;
&lt;body&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
text=&quot;&lt;bookstore&gt;&quot;
text=text+&quot;&lt;book&gt;&quot;;
text=text+&quot;&lt;title&gt;Everyday Italian&lt;/title&gt;&quot;;
text=text+&quot;&lt;author&gt;Giada De Laurentiis&lt;/author&gt;&quot;;
text=text+&quot;&lt;year&gt;2005&lt;/year&gt;&quot;;
text=text+&quot;&lt;/book&gt;&quot;;
text=text+&quot;&lt;/bookstore&gt;&quot;;

try //Internet Explorer
  {
  xmlDoc=new ActiveXObject(&quot;Microsoft.XMLDOM&quot;);
  xmlDoc.async=&quot;false&quot;;
  xmlDoc.loadXML(text);
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(text,&quot;text/xml&quot;);
    }
  catch(e) {alert(e.message)}
  }
document.write(&quot;xmlDoc is loaded, ready for use&quot;);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
</td></tr>
</table>

<p><a target="_blank" href="tryit.asp@filename=try_dom_parser2">Try it 
yourself</a></p>

<p><b>Note:</b> Internet Explorer uses the loadXML() method to parse an XML 
string, while other browsers uses the DOMParser object.</p>


<hr />

<a href="dom_nodetree.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_loadxmldoc.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>

<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -