Maison  >  Article  >  développement back-end  >  Apprentissage XML (1) Éléments, attributs, lecture détaillée

Apprentissage XML (1) Éléments, attributs, lecture détaillée

黄舟
黄舟original
2017-03-20 16:53:531422parcourir
  1. élément avec l'attribut

  2. javascript lit XML fichier :

<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>

Opération 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>

Remarquesimportantes

xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue
  • xmlDoc - Document XML créé par l'analyseur

  • getElementsByTagName("to")[0] - premier élément 05034471df6398a62d5708f78b78e0ac >

    childNodes[0] - le premier élément enfant de l'élément 05034471df6398a62d5708f78b78e0ac (le nœud de texte)
  • nodeValue - la valeur du nœud (le texte lui-même)
  • Question :
Si le fichier xml est :

Lire le premier5c3f7ccec2b1f8956d87810827641d62

<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>

Lire Prenez le deuxième 5c3f7ccec2b1f8956d87810827641d62

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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn