>  기사  >  백엔드 개발  >  XML 학습(1) 요소, 속성, 상세 읽기

XML 학습(1) 요소, 속성, 상세 읽기

黄舟
黄舟원래의
2017-03-20 16:53:531430검색
  1. 요소에 속성

  2. 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&#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 연산 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 - 파서

  • getElementsByTagName("to")[0]에 의해 생성된 XML 문서 - 첫 번째 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&#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>

첫 번째 읽기

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

두 번째 읽기

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

위 내용은 XML 학습(1) 요소, 속성, 상세 읽기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.