Rumah > Artikel > pembangunan bahagian belakang > XML学习(一)元素,属性,读取详解
元素 与 属性
javascript读取XML文件:
<p><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;"> </span><span style="color: #0000ff;"><?</span><span style="color: #ff00ff;">xml version="1.0" encoding="ISO-8859-1" </span><span style="color: #0000ff;">?></span><span style="color: #000000;"> <br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">note</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">to</span><span style="color: #0000ff;">></span><span style="color: #000000;">duncan<br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">to</span><span style="color: #0000ff;">></span><span style="color: #000000;"> <br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">from</span><span style="color: #0000ff;">></span><span style="color: #000000;">John</span><span style="color: #0000ff;"></</span><span style="color: #800000;">from</span><span style="color: #0000ff;">></span><span style="color: #000000;"> <br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;"><a href="http://www.php.cn/html/html-HEAD-2.html" target="_blank">head</a>ing</span><span style="color: #0000ff;">></span><span style="color: #000000;">Reminder</span><span style="color: #0000ff;"></</span><span style="color: #800000;">heading</span><span style="color: #0000ff;">></span><span style="color: #000000;"> <br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #000000;">Don't <a href="http://www.php.cn/wiki/125.html" target="_blank">for</a>get the meeting!</span><span style="color: #0000ff;"></</span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #000000;"> <br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">note</span><span style="color: #0000ff;">></span></p>
XML DOM 操作XML
<p><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff;"><</span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">script </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/javascript"</span><span style="color: #0000ff;">></span><span style="background-color: #f5f5f5; color: #000000;"><br/></span><span style="background-color: #f5f5f5; color: #0000ff;">function</span><span style="background-color: #f5f5f5; color: #000000;"> parseXML()<br/>{<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">try</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">Internet Explorer</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;"> {<br/> xmlDoc</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #0000ff;"><a href="http://www.php.cn/wiki/165.html" target="_blank">new</a></span><span style="background-color: #f5f5f5; color: #000000;"> ActiveX<a href="http://www.php.cn/wiki/60.html" target="_blank">Object</a>(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">Microsoft.XMLDOM</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);<br/> }<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">catch</span><span style="background-color: #f5f5f5; color: #000000;">(e)<br/> {<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">try</span><span style="background-color: #f5f5f5; color: #000000;"> </span><span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">Firefox, Mozilla, Opera, etc.</span><span style="background-color: #f5f5f5; color: #008000;"><br/></span><span style="background-color: #f5f5f5; color: #000000;"> {<br/> xmlDoc</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"><a href="http://www.php.cn/code/658.html" target="_blank">document</a>.implementation.createDocument(</span><span style="background-color: #f5f5f5; color: #000000;">""</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">""</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #0000ff;"><a href="http://www.php.cn/wiki/62.html" target="_blank">null</a></span><span style="background-color: #f5f5f5; color: #000000;">);<br/> }<br/></span><span style="background-color: #f5f5f5; color: #0000ff;">catch</span><span style="background-color: #f5f5f5; color: #000000;">(e)<br/> {<br/> alert(e.message);<br/></span><span style="background-color: #f5f5f5; color: #0000ff;"><a href="http://www.php.cn/wiki/135.html" target="_blank">return</a></span><span style="background-color: #f5f5f5; color: #000000;">;<br/> }<br/> }<br/>xmlDoc.async</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #0000ff;">false</span><span style="background-color: #f5f5f5; color: #000000;">;<br/>xmlDoc.load(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">note.xml</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);<br/><br/>document.getElementById(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">to</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">).innerHTML</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"><br/>xmlDoc.<a href="http://www.php.cn/code/8145.html" target="_blank">getElementsByTagName</a>(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">to</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">)[</span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">].childNodes[</span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">].nodeValue;<br/>document.getElementById(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">from</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">).innerHTML</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"><br/>xmlDoc.getElementsByTagName(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">from</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">)[</span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">].childNodes[</span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">].nodeValue;<br/>document.getElementById(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">message</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">).innerHTML</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;"><br/>xmlDoc.getElementsByTagName(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">body</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">)[</span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">].childNodes[</span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">].nodeValue;<br/>}<br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">body </span><span style="color: #ff0000;"><a href="http://www.php.cn/wiki/1467.html" target="_blank">onload</a></span><span style="color: #0000ff;">="parseXML()"</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">h1</span><span style="color: #0000ff;">></span><span style="color: #000000;">W3School.com.cn Internal Note</span><span style="color: #0000ff;"></</span><span style="color: #800000;">h1</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">><</span><span style="color: #800000;">b</span><span style="color: #0000ff;">></span><span style="color: #000000;">To:</span><span style="color: #0000ff;"></</span><span style="color: #800000;">b</span><span style="color: #0000ff;">></span><span style="color: #000000;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="to"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">><</span><span style="color: #800000;">br </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">b</span><span style="color: #0000ff;">></span><span style="color: #000000;">From:</span><span style="color: #0000ff;"></</span><span style="color: #800000;">b</span><span style="color: #0000ff;">></span><span style="color: #000000;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="from"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">><</span><span style="color: #800000;">br </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">b</span><span style="color: #0000ff;">></span><span style="color: #000000;">Message:</span><span style="color: #0000ff;"></</span><span style="color: #800000;">b</span><span style="color: #0000ff;">></span><span style="color: #000000;"> </span><span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="message"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></p>
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue
xmlDoc -由解析器创建的 XML 文档
getElementsByTagName("to")[0] - 第一个 05034471df6398a62d5708f78b78e0ac 元素
childNodes[0] - 05034471df6398a62d5708f78b78e0ac 元素的第一个子元素(文本节点)
nodeValue - 节点的值(文本本身)
疑问:
如果xml文件为:
<p><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;"> </span><span style="color: #0000ff;"><?</span><span style="color: #ff00ff;">xml version="1.0" encoding="ISO-8859-1" </span><span style="color: #0000ff;">?></span><span style="color: #000000;"> <br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">note</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">to</span><span style="color: #0000ff;">></span><span style="color: #000000;">asdfsd<br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">too</span><span style="color: #0000ff;">></span><span style="color: #000000;">duncan1</span><span style="color: #0000ff;"></</span><span style="color: #800000;">too</span><span style="color: #0000ff;">></span><span style="color: #000000;"> <br/><br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">to</span><span style="color: #0000ff;">></span><span style="color: #000000;"> <br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">too</span><span style="color: #0000ff;">></span><span style="color: #000000;">duncan2</span><span style="color: #0000ff;"></</span><span style="color: #800000;">too</span><span style="color: #0000ff;">></span><span style="color: #000000;"><br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">from</span><span style="color: #0000ff;">></span><span style="color: #000000;">John</span><span style="color: #0000ff;"></</span><span style="color: #800000;">from</span><span style="color: #0000ff;">></span><span style="color: #000000;"> <br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">heading</span><span style="color: #0000ff;">></span><span style="color: #000000;">Reminder</span><span style="color: #0000ff;"></</span><span style="color: #800000;">heading</span><span style="color: #0000ff;">></span><span style="color: #000000;"> <br/></span><span style="color: #0000ff;"><</span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #000000;">Don't forget the meeting!</span><span style="color: #0000ff;"></</span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #000000;"> <br/></span><span style="color: #0000ff;"></</span><span style="color: #800000;">note</span><span style="color: #0000ff;">></span></p>
读取第一个77a74ca8c1d5e9a5096e217b38b998c4
xmlDoc.getElementsByTagName("to")[0].getElementsByTagName("t00")[0].childNodes[0].nodeValue
读取第二个77a74ca8c1d5e9a5096e217b38b998c4
xmlDoc.getElementsByTagName("too")[0].childNodes[0].nodeValue
Atas ialah kandungan terperinci XML学习(一)元素,属性,读取详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!