php 與 XML


此篇為備忘錄


假設有個XML檔長這樣:

<ROOT>
 <A ATTR1="value1" ATTR2="value2">
  <B>Hello</B>
  <B>Again</B>
  <C ATTR3="value3">Bye Bye</C>
 </A>
 <A ATTR1="value4" ATTR2="value5">
  <B>This</B>
  <B>is a test</B>
  <C ATTR3="value6">See you next time!</C>
 </A>
</ROOT>

PHP的程式碼長這樣:
<?php
 $url="test.xml";
 $xml = simplexml_load_file($url);
 echo (string)$xml->A[0]->B[0]."<BR>";
 echo (string)$xml->A[0]->B[1]."<BR>";
 echo (string)$xml->A[1]->B[0]."<BR>";
 echo (string)$xml->A[1]->B[1]."<BR>";
 echo (string)$xml->A[1]->C[0]."<BR>";
 echo (string)$xml->A[0]["ATTR1"]."<BR>";
 echo (string)$xml->A[0]["ATTR2"]."<BR>";
 echo (string)$xml->A[0]->C[0]["ATTR3"]."<BR>";
 echo (string)$xml->A[1]["ATTR1"]."<BR>";
 echo (string)$xml->A[1]["ATTR2"]."<BR>";
 echo (string)$xml->A[1]->C[0]["ATTR3"]."<BR>";
?>
source : http://wp.mlab.tw/?p=1790

沒有留言:

張貼留言