อ่าน xml ทั้งที ไม่มี xPath ได้ไง
เกริ่นกันก่อน หลายๆคนอาจจะเริ่ม ตะหงิดๆ ทำไมพักนี้ผม อัพเดทอะไร หลายๆอย่างไม่ได้ เกี่ยวกับ jQuery เลย นั่นก็เพราะว่า “นึกไม่ออก” นั่นเอง (-*-)
jQuery มันเอาไว้ประยุกต์ใช้งาน มีแค่ Basic ก็สามารถทำได้ทุกอย่างแล้ว ที่เหลือก็หา plugin มานั่งแกะๆ เอาก็จะได้ทักษะเพิ่มไปเรื่อยๆ เอง ยิ่งใครมีพื้น js แน่นอยู่แล้ว แทบจะเรียกว่า ของหมูๆเลย ฉะนั้นในระหว่างที่ยังนึก ว่าจะอัพไรดีไม่ออก ผมก็จะเอาความรู้ ที่พอจะมีตกค้างมาบ้าง มาอัพเดทแทนไปพลางๆ 555
แต่ถ้าใครอยากรู้หรือสนใจอะไรเพิ่มเติมเกี่ยวกับ jQuery ก็หลังไมค์ มาได้ครับ ถ้ารู้ก็จะเอามาอัพเดทให้ แต่ถ้าไม่รู้ผมก็จะแกล้งทำเป็นรู้ แล้วไปหาบทความชาว ต่างชาติต่างเมือง มาแปลเป็นไทยให้ WoW!
เอาล่ะๆ เริ่มอะไรที่มันมีเนื้อหาสาระกันดีกว่า xPath เป็นภาษาที่เอาไว้ ไล่ nodes ในเอกสาร XML (จริงๆ ใน json ก็มีคนเอามาประยุกต์เลียนแบบ) ซึ่งอยากบอกว่ามันเป็นอะไรที่ โครตสะดวกเลย ลองมาดูตัวอย่างกันดีกว่า
XML File: simple.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<rss>
-
<book id="bk01">
-
<author>Mr.Tee++;</author>
-
<title>jQuery Developer’s Guide</title>
-
<genre>Computer</genre>
-
<price>44.95</price>
-
</book>
-
<book id="bk02">
-
<author>Mr.Tee++;</author>
-
<title>XML Developer’s Guide</title>
-
<genre>Computer</genre>
-
<price>5.95</price>
-
</book>
-
<book id="bk03">
-
<author>Not me</author>
-
<title>To be a hacker</title>
-
<genre>Internet</genre>
-
<price>10.50</price>
-
</book>
-
<book id="bk04">
-
<author>Mr.Tee++;</author>
-
<title>Google Adsense</title>
-
<genre>SEO</genre>
-
<price>70</price>
-
</book>
-
</rss>
สมมุติผมมี ตัวอย่าง xml ดังชุดข้างบน ผมจะลองมาดูว่าผมจะหาข้อมูลของ xml ชุดนี้โดยใช้ xPath เข้าช่วยได้อย่างไรบ้าง
-
<strong>PHP file: xpathdemo.php</strong>
-
$xml = simplexml_load_file(’simple.xml’);
ตัวอย่างที่ 1 การหาค่า attribute = “xxx”
-
$res = $xml->xPath(‘//book[@id="bk01"]‘);
-
/* ผลลัพธ์
-
Array
-
(
-
[0] => SimpleXMLElement Object
-
(
-
[@attributes] => Array
-
(
-
[id] => bk01
-
)
-
-
[author] => Mr.Tee++;
-
[title] => jQuery Developer’s Guide
-
[genre] => Computer
-
[price] => 44.95
-
)
-
-
)*/
ตัวอย่างนี้จะเป็นการหา author ที่ไม่ใช้ Not me ดังนั้นผลลัพธ์ จะได้ออกมา 3 records ซึ่ง author จะเท่ากับ Mr.Tee++;
-
$res = $xml->xPath(‘//author[not(.="Not me")]‘);
-
/* ผลลัพธ์
-
Array
-
(
-
[0] => SimpleXMLElement Object
-
(
-
[0] => Mr.Tee++;
-
)
-
-
[1] => SimpleXMLElement Object
-
(
-
[0] => Mr.Tee++;
-
)
-
-
[2] => SimpleXMLElement Object
-
(
-
[0] => Mr.Tee++;
-
)
-
-
)*/
ตัวอย่างนี้จะเป็นการหา price ที่มากกว่า 40 ขึ้นไป รวมทั้งสั่งให้ point node ไปที่ parent ของมันซึ่งก็คือ หลังจากที่มันหาค่าได้ node จะชี้ไปอยู่ที่ price แต่ผมต้องการ fields อื่นที่เป็น siblings ของมันด้วย
-
$res = $xml->xPath(‘//price[.>40]/parent::*’);
-
-
/* ผลลัพธ์
-
Array
-
(
-
[0] => SimpleXMLElement Object
-
(
-
[@attributes] => Array
-
(
-
[id] => bk01
-
)
-
-
[author] => Mr.Tee++;
-
[title] => jQuery Developer’s Guide
-
[genre] => Computer
-
[price] => 44.95
-
)
-
-
[1] => SimpleXMLElement Object
-
(
-
[@attributes] => Array
-
(
-
[id] => bk04
-
)
-
-
[author] => Mr.Tee++;
-
[title] => Google Adsense
-
[genre] => SEO
-
[price] => 70
-
)
-
-
)
-
-
สุดท้ายเป็นตัวอย่างการ Count ค่า 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.
ใช้ง่ายจิงๆๆ
[...] หน้านี้แล้วลองไปอ่านดูกันได้ ที่นี่ แต่ตอนนั้นมันคือ PHP ครับ ตอนนี้ [...]