Home  >  Article  >  Backend Development  >  XML Learning (1) Elements, Attributes, Detailed Reading

XML Learning (1) Elements, Attributes, Detailed Reading

黄舟
黄舟Original
2017-03-20 16:53:531421browse
  1. Elements and Attributes

  2. ##javascriptRead XML file:

  3. <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&#39;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 Operation 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>

Important

Comments
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue

  • xmlDoc - XML ​​document created by parser

  • getElementsByTagName("to")[0] - first 05034471df6398a62d5708f78b78e0ac element

  • childNodes[0] - the first child of the 05034471df6398a62d5708f78b78e0ac element (the text node)

  • nodeValue - the value of the node (the text itself)

Question:

If the xml file is:

<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&#39;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>

Read the first 77a74ca8c1d5e9a5096e217b38b998c4

xmlDoc.getElementsByTagName("to")[0].getElementsByTagName("t00")[0].childNodes[0].nodeValue

Read the second 77a74ca8c1d5e9a5096e217b38b998c4


xmlDoc.getElementsByTagName("too")[0].childNodes[0].nodeValue

The above is the detailed content of XML Learning (1) Elements, Attributes, Detailed Reading. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn