XML学习(一)元素,属性,读取详解
xml encoding="utf-8" ?>
元素 与 属性
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;">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>
<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;">new</span><span style="background-color: #f5f5f5; color: #000000;"> ActiveXObject(</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;">document.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;">null</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;">return</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.getElementsByTagName(</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;">onload</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
getElementsByTagName("to")[0] - 第一个
元素 childNodes[0] -
元素的第一个子元素(文本节点) 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>
读取第一个
xmlDoc.getElementsByTagName("to")[0].getElementsByTagName("t00")[0].childNodes[0].nodeValue
读取第二个
xmlDoc.getElementsByTagName("too")[0].childNodes[0].nodeValue
以上就是XML学习(一)元素,属性,读取详解的详细内容,更多请关注其它相关文章!