อ่าน xml ทั้งที ไม่มี xPath ได้ไง

เกริ่นกันก่อน หลายๆคนอาจจะเริ่ม ตะหงิดๆ ทำไมพักนี้ผม อัพเดทอะไร หลายๆอย่างไม่ได้ เกี่ยวกับ jQuery เลย นั่นก็เพราะว่า “นึกไม่ออก” นั่นเอง (-*-)

jQuery มันเอาไว้ประยุกต์ใช้งาน มีแค่ Basic ก็สามารถทำได้ทุกอย่างแล้ว ที่เหลือก็หา plugin มานั่งแกะๆ เอาก็จะได้ทักษะเพิ่มไปเรื่อยๆ เอง ยิ่งใครมีพื้น js แน่นอยู่แล้ว แทบจะเรียกว่า ของหมูๆเลย ฉะนั้นในระหว่างที่ยังนึก ว่าจะอัพไรดีไม่ออก ผมก็จะเอาความรู้ ที่พอจะมีตกค้างมาบ้าง มาอัพเดทแทนไปพลางๆ 555

แต่ถ้าใครอยากรู้หรือสนใจอะไรเพิ่มเติมเกี่ยวกับ jQuery ก็หลังไมค์ มาได้ครับ ถ้ารู้ก็จะเอามาอัพเดทให้ แต่ถ้าไม่รู้ผมก็จะแกล้งทำเป็นรู้ แล้วไปหาบทความชาว ต่างชาติต่างเมือง มาแปลเป็นไทยให้ WoW!

เอาล่ะๆ เริ่มอะไรที่มันมีเนื้อหาสาระกันดีกว่า xPath เป็นภาษาที่เอาไว้ ไล่ nodes ในเอกสาร XML (จริงๆ ใน json ก็มีคนเอามาประยุกต์เลียนแบบ) ซึ่งอยากบอกว่ามันเป็นอะไรที่ โครตสะดวกเลย ลองมาดูตัวอย่างกันดีกว่า

XML File: simple.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <rss>
  3. <book id="bk01">
  4.         <author>Mr.Tee++;</author>
  5.         <title>jQuery Developer’s Guide</title>
  6.         <genre>Computer</genre>
  7.         <price>44.95</price>
  8. </book>
  9. <book id="bk02">
  10.         <author>Mr.Tee++;</author>
  11.         <title>XML Developer’s Guide</title>
  12.         <genre>Computer</genre>
  13.         <price>5.95</price>
  14. </book>
  15. <book id="bk03">
  16.         <author>Not me</author>
  17.         <title>To be a hacker</title>
  18.         <genre>Internet</genre>
  19.         <price>10.50</price>
  20. </book>
  21. <book id="bk04">
  22.         <author>Mr.Tee++;</author>
  23.         <title>Google Adsense</title>
  24.         <genre>SEO</genre>
  25.         <price>70</price>
  26. </book>
  27. </rss>

สมมุติผมมี ตัวอย่าง xml ดังชุดข้างบน ผมจะลองมาดูว่าผมจะหาข้อมูลของ xml ชุดนี้โดยใช้ xPath เข้าช่วยได้อย่างไรบ้าง

  1. <strong>PHP file: xpathdemo.php</strong>
  2. $xml = simplexml_load_file(’simple.xml’);

ตัวอย่างที่ 1 การหาค่า attribute = “xxx”

  1. $res = $xml->xPath(‘//book[@id="bk01"]‘);
  2. /* ผลลัพธ์
  3. Array
  4. (
  5.     [0] => SimpleXMLElement Object
  6.         (
  7.             [@attributes] => Array
  8.                 (
  9.                     [id] => bk01
  10.                 )
  11.  
  12.             [author] => Mr.Tee++;
  13.             [title] => jQuery Developer’s Guide
  14.             [genre] => Computer
  15.             [price] => 44.95
  16.         )
  17.  
  18. )*/

ตัวอย่างนี้จะเป็นการหา author ที่ไม่ใช้ Not me ดังนั้นผลลัพธ์ จะได้ออกมา 3 records ซึ่ง author จะเท่ากับ Mr.Tee++;

  1. $res = $xml->xPath(‘//author[not(.="Not me")]‘);
  2. /* ผลลัพธ์
  3. Array
  4. (
  5.     [0] => SimpleXMLElement Object
  6.         (
  7.             [0] => Mr.Tee++;
  8.         )
  9.  
  10.     [1] => SimpleXMLElement Object
  11.         (
  12.             [0] => Mr.Tee++;
  13.         )
  14.  
  15.     [2] => SimpleXMLElement Object
  16.         (
  17.             [0] => Mr.Tee++;
  18.         )
  19.  
  20. )*/

ตัวอย่างนี้จะเป็นการหา price ที่มากกว่า 40 ขึ้นไป รวมทั้งสั่งให้ point node ไปที่ parent ของมันซึ่งก็คือ หลังจากที่มันหาค่าได้ node จะชี้ไปอยู่ที่ price แต่ผมต้องการ fields อื่นที่เป็น siblings ของมันด้วย

  1. $res = $xml->xPath(‘//price[.>40]/parent::*’);
  2.  
  3. /* ผลลัพธ์
  4. Array
  5. (
  6.     [0] => SimpleXMLElement Object
  7.         (
  8.             [@attributes] => Array
  9.                 (
  10.                     [id] => bk01
  11.                 )
  12.  
  13.             [author] => Mr.Tee++;
  14.             [title] => jQuery Developer’s Guide
  15.             [genre] => Computer
  16.             [price] => 44.95
  17.         )
  18.  
  19.     [1] => SimpleXMLElement Object
  20.         (
  21.             [@attributes] => Array
  22.                 (
  23.                     [id] => bk04
  24.                 )
  25.  
  26.             [author] => Mr.Tee++;
  27.             [title] => Google Adsense
  28.             [genre] => SEO
  29.             [price] => 70
  30.         )
  31.  
  32. )
  33.  
  34.  

สุดท้ายเป็นตัวอย่างการ Count ค่า genre = computer ซึ่งไม่มีอะไรมากทำแค่นี้ (เริ่มขี้เกียจอธิบาย)^^

  1. echo count($xml->xPath(‘//genre[.="Computer"]‘));

เอาล่ะ นี่เป็นแค่น้ำจิ้มจริงๆ xPath ทำไรได้เยอะกว่านี้แน่นอน ลองไปถามพี่ Goo เค้าสิ ท่าไม่เชื่อ วันนี้ลาไปก่อน หวัดดี

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Comments »

 
 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>