เอ็กเอ็มแอล (XML : eXtensible Markup Language)
เว็บเพจสำรอง (Backup Webpages) : thaiall.com | lampang.thcity.com
ปรับปรุง : 2552-03-04 (ปรับปรุง family.xml)
ความแตกต่างของ HTML และ XML
HTML คือ ภาษาสำหรับแสดงผลข้อมูล และแสดงข้อมูลอย่างไร
XML คือ ภาษาสำหรับอธิบายข้อมูล และเก็บข้อมูลอะไร
XML Meaning
XML คือรูปแบบข้อมูลที่มีโครงสร้างที่เอื้อต่อการแลกเปลี่ยนข้อมูลในเครือข่ายอินเทอร์เน็ต เป็นมาตรฐานที่ถูกกำหนดโดย W3C หรือ XML (eXtensible Markup Language) is a data format for structured document interchange on the Web. It is a standard defined by The World Wide Web consortium (W3C). Information about XML and related technologies can be found at http://www.w3.org/XML/
    ข้อควรทราบ
  1. xml คือ แฟ้มข้อมูลแบบ text file ลักษณะหนึ่ง คล้ายแฟ้มแบบ CSV(Comma separated variable) ที่ท่านต้องทำคือศึกษาวิธีอ่านข้อมูลมาใช้
  2. xml คือ แฟ้มข้อมูลที่เก็บในเว็บของเจ้าของข้อมูล แต่ท่านสามารถพัฒนาโปรแกรมอ่านข้อมูลด้วยภาษาใด ๆ เพื่อดึงข้อมูลมาแสดงในเว็บท่านได้ แม้ท่านไม่ใช้เจ้าของข้อมูล
  3. xml เท่าที่ผมทราบ xml คือข้อมูลที่ มีลักษณะแบบ readonly ในมุมมองของผู้ใช้ทั่วไป ที่จะนำข้อมูลไปใช้
  4. xml ได้รับการยอมรับ จากภาษา และระบบปฏิบัติการต่าง ๆ งานของท่าน คือ ศึกษาภาษาเหล่านั้นว่าจะนำ xml ไปใช้อะไร
  5. xml มีเทคโนโลยีสนับสนุนเยอะมาก ในบทความนี้เพียงแต่ใช้ xsl, dom และ php อ่านข้อมูลอย่างง่าย ๆ เท่านั้น
XML :: ที่ผมได้ศึกษา มีลักษณะเหมือนกับแฟ้มข้อมูลคล้าย dbf, mdb เป็นต้น แต่แฟ้ม xml เป็นที่ยอมรับ และนำไปใช้ได้กับทุกระบบปฏิบัติการ โดยผู้ใช้สามารถเขียนโปรแกรมเข้าไปดึงข้อมูล และใช้กับอุปกรณ์ต่าง ๆ ได้มากมาย โดยตัวอย่างด้านล่างทั้งหมด เป็นการใช้หลักการของ xsl เพื่อจัดรูปแบบข้อมูล และใช้ html เรียกข้อมูลไปแสดงผล โดยใช้ฟังก์ชัน และคำสั่งเพียงไม่กี่คำสั่ง
    ลักษณะแฟ้มแต่ละแบบประกอบบทเรียนนี้
  1. แฟ้ม xml :: ใช้เก็บข้อมูล
  2. แฟ้ม xsl :: ใช้แต่งรูปแบบ (แต่ผมยังมีปัญหาใช้ html บางคำสั่งใช้ไม่ได้เช่น /td หรือ /ol เป็นต้น)
  3. แฟ้ม htm :: เป็นบทสรุปในการเรียก xml และ xsl มาใช้ร่วมกัน
  4. แฟ้ม php :: เป็นโปรแกรม Server script เพื่อเรียก .xml มาประมวลผลขั้นสูง
    การทดสอบทั้งหมดใช้ ie5 และทำกับเครื่องที่เป็น standalone ที่ไม่ได้ลงโปรแกรม server ใด ๆ
    แนะนำเว็บเกี่ยวกับ XML
  1. http://www.thaixml.com
  2. http://www.devguru.com/Technologies/
  3. http://www.xmlsoft.org/guidelines.html
  4. http://www.xml.org
  5. http://www.php.net/manual/en/ref.xml.php
  6. http://msdn.microsoft.com/library
  7. http://www.thaixml.com
  8. http://www.xml.com
  9. http://www.zvon.org
  10. http://fr.selfhtml.org/javascript
  11. http://www-106.ibm.com/developerworks/
  12. http://www.topxml.com
  13. http://www.vcharkarn.com/include/article/
  14. http://ict.moph.go.th/project/xml/ ***
  15. http://www.richnetapps.com .zip ?
  16. http://www.maani.us/xml_charts/ .zip
  17. http://www.thaiall.com/vbnet/data_conn.htm ***
    ตัวอย่างแฟ้ม XML ใน internet
  1. http://thaiall.com/xml/family.xml
  2. http://truehits.net/xml/education.xml
  3. http://www.arip.co.th/rss/rss_news.xml
  4. http://ict.moph.go.th/project/xml/export_xml_1.php
  5. http://lampang.thcity.com/xml/employees.xml
  6. http://lampang.thcity.com/xml/xmlproducts.xml

    สารบัญ
    Section 1 : How to write XML file
  1. Data file : employees.xml [ดู source code]
    :: ดูแฟ้มนี้ด้วย browser ด้วยการ click employees.xml
    :: แฟ้นนี้คือ แฟ้มข้อมูล ที่เปิดให้ load ไปใช้อย่างไรก็ได้ แต่เน้นไปในทาง readonly ผมได้สร้างแฟ้มนี้ และเขียนโปรแกรม list01.xsl, list01.htm, list02.xsl, list02.htm, list03sale.xsl, list03admin.xsl, list03.htm ซึ่งทั้งหมดทำงานในเครื่อง stand alone และใช้ IE5 ขึ้นไป เรียกข้อมูลมาแสดงได้ปกติ
    :: ประโยชน์ที่เห็นได้ชัดเจนของ XML คือ สามารถนำไปแสดงในระบบปฏิบัติ อุปกรณ์ หรือสื่อต่าง ๆ ได้โดยง่าย เพราะได้รับการยอมรับอย่างกว้างขวาง (ขนาดผมเองยังมีแผนนำเสนอข้อมูลทั้งหมด ที่เคยจัดเก็บในรูป xml) เพราะท่านสามารถเขียน xsl จัดรูปแบบข้อมูลใหม่ แต่เรียกข้อมูลจากเว็บของผม คล้ายตัวอย่าง educationt.xsl และ educationt.htm
    Section 2 : Using of XSL and HTM (Case 1)
  2. list01.xsl (ใช้ข้อมูลจาก employees.xml) [ดู source code]
    :: กำหนดรูปแบบ extensible stylesheet language เพื่อให้โปรแกรมอื่น ๆ เรียกไปใช้ และกำหนดรูปแบบได้ตามที่กำหนด ในที่นี้จะถูกเรียกใช้โดย list01.htm ผลที่ได้จะเป็นข้อมูลธรรมดาที่ไม่ถูกจัดเรียก และท่านสามารถทดสอบโปรแกรมเพื่อดูผลได้จาก list01.htm
  3. list01.htm (ใช้ข้อมูลจาก employees.xml) [ดู source code]
    :: ทดสอบที่ [thaiall.com และ thcity.com]
    :: โปรแกรมนี้ใช้ java script ในการเรียกแฟ้มข้อมูล employees.xml และเรียกรูปแบบจาก list01.xsl มารวมกัน และให้ผลตามที่ต้องการในโปรแกรมตัวนี้
    Section 3 : Using of XSL and HTM (Case 2)
  4. list02.xsl (ใช้ข้อมูลจาก employees.xml) [ดู source code]
  5. list02.htm (ใช้ข้อมูลจาก employees.xml) [ดู source code]
    :: ทดสอบที่ [thaiall.com และ thcity.com]
    Section 4 : Using of XSL and HTM (Case 3)
  6. list03sale.xsl (ใช้ข้อมูลจาก employees.xml) [ดู source code]
    :: เลือกเฉพาะข้อมูลที่มี department = sale
  7. list03admin.xsl (ใช้ข้อมูลจาก employees.xml) [ดู source code]
    :: เลือกเฉพาะข้อมูลที่มี department = admin
  8. list03.htm (ใช้ข้อมูลจาก employees.xml) [ดู source code]
    :: ทดสอบที่ [thaiall.com และ thcity.com]
    :: มีการใช้ความสามารถของ java script เพื่อส่งความต้องการจาก form ไปเลือก xsl ที่ต้องการ
    Section 5 : Using of XSL and HTM (Case 4)
  9. Data file : http://truehits.net/xml/education.xml [ดู source code]
    :: คัดลอกแฟ้มนี้มาจาก truehits.net เมื่อวันที่ 3 กันยายน 2545
  10. education.xsl (ใช้ข้อมูลจาก education.xml) [ดู source code]
  11. education.htm (ใช้ข้อมูลจาก education.xml) [ดู source code]
    :: ทดสอบที่ [thaiall.com และ thcity.com]
  12. educationt.htm (ใช้ข้อมูลจาก http://truehits.net/xml/education.xml) [ดู source code]
    :: ทดสอบตามปกติที่ thcity.com และ thaiall.com ไม่ได้ ถ้าให้ได้ จะต้องลด Security ของ Browser เป็น Low
    :: โปรแกรมนี้เรียกรูปแบบ(xsl) ใน server ของผม แต่ไปเรียกข้อมูลจากเจ้าของข้อมูลที่ http://truehits.net/xml/education.xml ท่านอาจประยุกต์ด้วยการ เขียน xsl และ htm ของตนเอง แต่เรียกข้อมูลที่แหล่งข้อมูลเปิดเผยให้ใช้ ในอนาคต ผมจะเริ่มนำข้อมูลมาให้ใช้ในแบบ xml บ้าง เช่นฐานข้อมูลเว็บของชาวไทย เป็นต้น และโปรแกรมนี้เองทำให้ผมดูผลการจัดอันดับของกลุ่มการศึกษา ของ truehits.net โดยไม่ต้องเข้าไปเปิดเว็บไซต์ของเขา
    :: ผมได้รับ e-mail จากคุณศุภกิจ pao@eng.kmitnb.ac.th ทำให้ทราบว่าตัวอย่างนี้ทดสอบแล้วมีปัญหาคืออ่าน xml จากข้างนอกเข้ามาไม่ได้ แต่ถ้าทดสอบกับเครื่องตนเองจะอ่าน xml จากที่ใดก็ได้ เมื่อผมทดสอบก็พบปัญหาเช่นกัน แต่พบวิธีแก้ไขคือแก้ option ใน IE เรื่อง security ให้เป็น Low ก็จะใช้ตัวอย่างที่ 12 นี้ได้ ที่ผมหาปัญหาพบ เพราะลองติดตั้ง PWS แล้วเรียกใช้ จะมีการถามเรื่องความปลอดภัยซึ่งหลักการนี้มีปัญหานะครับ หรือไม่ก็ต้องไปหา PHP ตามตัวอย่าง 15 ครับ สำหรับการลด security ผมมีภาพมาฝาก แสดงในส่วน source code ด้วย
    Section 6 : Using of XML and PHP
  13. structure.php (ใช้ข้อมูลจาก http://truehits.net/xml/education.xml หรือท่านกำหนดขึ้นเองได้) [ดู source code]
    :: ทดสอบที่ [thcity.com] แต่ thaiall.com ใช้ structure.php แล้วอ่าน XML ไม่ได้ ปัญหาน่าจะมามาก server
  14. readxml.php (ใช้ข้อมูลจาก http://truehits.net/xml/education.xml หรือท่านกำหนดขึ้นเองได้) [ดู source code]
    :: ทดสอบที่ [thcity.com] แต่ thaiall.com ใช้ readxml.php แล้วอ่าน XML ไม่ได้ ปัญหาน่าจะมามาก server
  15. xmldata.php (ใช้ข้อมูลจาก employees.xml หรือท่านกำหนดขึ้นเองได้) [ดู source code]
    :: ทดสอบที่ [thcity.com] แต่ thaiall.com ใช้ xmldata.php แล้วอ่าน XML ไม่ได้ ปัญหาน่าจะมามาก server
    :: โปรแกรมนี้ถูกปรับปรุงให้สามารถอ่าน xml ได้โดยไม่ต้องทราบโครงสร้างของแฟ้มก่อน แต่อ่านมาแสดงผลในตารางได้ ได้ทดสอบกับหลาย ๆ แล้วของ xml แต่อาจมีปัญหากับบางแฟ้มที่มีโครงสร้างซับซ้อนได้ ... ต้องลองดูครับ
    Section 7 : javascript in HTML
  16. xmljavas.htm (ใช้ข้อมูลจาก dataj.xml หรือท่านกำหนดขึ้นเองได้) [ดู source code]
    :: ทดสอบที่ [thaiall.com และ thcity.com] ผลการทดสอบมีปัญหาเรื่อง security ให้ดูจากข้อ 12
    :: แฟ้ม XML บางแฟ้มมีระเบียนเดียว โปรแกรม Javascript นี้จะอ่านข้อมูลทั้งหมดไปใช้ได้ โดยไม่คำนึงว่ามีกี่ระเบียน มักเป็นข้อมูลที่เก็บข้อกำหนดต่าง ๆ เพียงระเบียนเดียวเท่านั้น ถ้ามีหลายระเบียนก็จะใช้หลักการในหัวข้อต่อไป
  17. xmledu.htm (ใช้ข้อมูลจาก http://truehits.net/xml/education.xml) [ดู source code]
    :: ทดสอบที่ [thaiall.com และ thcity.com] ผลการทดสอบมีปัญหาเรื่อง security ให้ดูจากข้อ 12
    :: แสดงการอ่านข้อมูลจากแฟ้ม http://truehits.net/xml/education.xml มาแสดงผลได้อย่างเหมาะสม โดยใช้ความสามารถของ javascript พร้อมแทรก html code เข้าไปในรายงานได้อย่างสวยงามเท่าที่ต้องการ
    Section 8 : Convert from CSV to XML
  18. crtxml.php (ทดสอบโปรแกรมที่ crtxml.php) [ดู source code]
    :: ทดสอบที่ [thaiall.com และ thcity.com]
    :: โปรแกรมนี้ช่วยให้ท่านที่มีข้อมูลในแบบ CSV (Comma Separated Variable) สามารถสร้างแฟ้ม XML มาใช้งานได้โดยง่าย โดยกำหนดชื่อของ field ต่าง ๆ พร้อมกรอกข้อมูล หลังกดปุ่ม submit ก็จะได้แฟ้ม XML ท่านเพียงแต่ save as โดยเลือก type เป็น HTML only แล้ว ตั้งชื่อใหม่มีนามสกุลเป็น .XML ก็จะนำแฟ้มนี้ไปใช้เป็น XML file ได้ทันที
    :: หรือ Right click ด้วย mouse บนแฟ้มที่มีรูปแบบเป็น XML แล้ว จากนั้นเลือก View source แล้ว Save as เป็นแฟ้มที่มีสกุลเป็น XML เพื่อนำไปใช้ต่อไป
    Section 999 : แปลงข้อมูลที่มีอยู่เป็น XML
    โปรแกรมต่าง ๆ ยอมรับแฟ้ม XML มากขึ้น ทำให้การ convert ข้อมูลที่มีอยู่เป็น XML ทำได้ง่ายขึ้น
    ตัวอย่าง 1 : ใช้ MS Access 2000 convert จากตาราง ได้ แฟ้ม xml xsd xsl มาพร้อมกันเลย

    ตัวอย่าง 2 : ใช้ MS Excel 2002 จัดเก็บเป็นแฟ้มแบบ xml (Excel รุ่นนี้เปิด XML มาแก้ไขได้เลย)

    ตัวอย่าง 3 : ใช้ PHPMyAdmin ส่งข้อมูลใน MySQL ไปเป็นแฟ้ม xml (Mysql-front ก็ทำได้เช่นกัน)

1. Data file : employees.xml
    <?xml version="1.0"?> <employee> <empl department="sale"> <emplid>45001</emplid> <emplname>thanom</emplname> <emplsalary>15000</emplsalary> </empl> <empl department="sale"> <emplid>45002</emplid> <emplname>pikun</emplname> <emplsalary>9999</emplsalary> </empl> <empl department="admin"> <emplid>45003</emplid> <emplname>sombat</emplname> <emplsalary>12500</emplsalary> </empl> </employee>

2. Source of : list01.xsl
    <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <xsl:for-each select="employee/empl"> <xsl:value-of/> </xsl:for-each> </xsl:template> </xsl:stylesheet>

3. Source of : list01.htm
    <html><head> <script language="javascript"> var xml,xsl,formatresult; xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = false; xml.load("employees.xml"); xsl = new ActiveXObject("Microsoft.XMLDOM"); xsl.async = false; xsl.load("list01.xsl"); formatresult = xml.transformNode(xsl); document.write(formatresult); </script></head> <body></body></html>

4. Source of : list02.xsl
    <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <xsl:for-each select="employee/empl"> <xsl:value-of select="@department"/>, <xsl:value-of select="emplid"/>, <xsl:value-of select="emplname"/>, <xsl:value-of select="emplsalary"/><br/> </xsl:for-each> </xsl:template> </xsl:stylesheet>

5. Source of : list02.htm
    <html><head> <script language="javascript"> var xml,xsl,formatresult; xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = false; xml.load("employees.xml"); xsl = new ActiveXObject("Microsoft.XMLDOM"); xsl.async = false; xsl.load("list02.xsl"); formatresult = xml.transformNode(xsl); document.write(formatresult); </script></head> <body></body></html>

6. Source of : list03sale.xsl
    <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <xsl:for-each select="employee/empl"> <xsl:if test="@department[.='sale']"> <xsl:value-of select="@department"/>, <xsl:value-of select="emplid"/>, <xsl:value-of select="emplname"/>, <xsl:value-of select="emplsalary"/><br/> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>

7. Source of : list03admin.xsl
    <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <xsl:for-each select="employee/empl"> <xsl:if test="@department[.='admin']"> <xsl:value-of select="@department"/> <xsl:value-of select="emplid"/>, <xsl:value-of select="emplname"/>, <xsl:value-of select="emplsalary"/><br/> </xsl:if> </xsl:for-each> </xsl:template> </xsl:stylesheet>

8. Source of : list03.htm
    <html><head> <script language="javascript"> var xml,xsl; xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = false; xml.load("employees.xml"); function changexsl(xslstyle) { alert(xslstyle); xsl = new ActiveXObject("Microsoft.XMLDOM"); xsl.async = false; xsl.load(xslstyle); result.innerHTML = xml.transformNode(xsl); } function filter(position) { if (position == "sale") changexsl("list03sale.xsl"); if (position == "admin") changexsl("list03admin.xsl"); } </script></head> <body> <form method=post><select name="position"> <option value="sale">sale <option value="admin">admin </select> <input type=button name=change value=select onclick="filter(position.value)"> </form> <div id="result"></div> </body></html>

9. Data file : http://truehits.net/xml/education.xml
    <?xml version="1.0" encoding="windows-874"?> <!DOCTYPE data [ <!ELEMENT data (member*)> <!ATTLIST data date CDATA #IMPLIED category CDATA #IMPLIED> <!ELEMENT member (url, website, sort, uniqueip, uniquesession, pageviews)> <!ELEMENT url (#PCDATA)> <!ELEMENT website (#PCDATA)> <!ELEMENT sort (#PCDATA)> <!ELEMENT uniqueip (#PCDATA)> <!ELEMENT uniquesession (#PCDATA)> <!ELEMENT pageviews (#PCDATA)> ]> <data date="2/09/2002" category="การศึกษา"> <member> <url>www.friends.co.th</url> <website>Friends.co.th - เพื่อนเก่า ทะเบียนรุ่น ทุกสถาบัน</website> <sort>10</sort> <uniqueip>14701</uniqueip> <uniquesession>50974</uniquesession> <pageviews>76436</pageviews> </member> <member> <url>www.ru.ac.th</url> <website>มหาวิทยาลัยรามคำแหง</website> <sort>68</sort> <uniqueip>3068</uniqueip> <uniquesession>4698</uniquesession> <pageviews>4976</pageviews> </member> <member> <url>www.stou.ac.th</url> <website>มหาวิทยาลัยสุโขทัยธรรมาธิราช</website> <sort>74</sort> <uniqueip>2905</uniqueip> <uniquesession>8621</uniquesession> <pageviews>13482</pageviews> </member> <member> <url>www.thaiall.com</url> <website>thaiall.com,สอบ,สอน,สืบค้น,บทความ,ภาพสวย,จัดอันดับ</website> <sort>114</sort> <uniqueip>1906</uniqueip> <uniquesession>5054</uniquesession> <pageviews>8201</pageviews> </member> <member> <url>www.NetDesign.ac.th</url> <website>NetDesign ศูนย์อบรมด้าน Web Design และ E-Commerce อันดับ1 ของเมืองไทย</website> <sort>138</sort> <uniqueip>1504</uniqueip> <uniquesession>4614</uniquesession> <pageviews>6769</pageviews> </member> <member> <url>www.eduzones.com</url> <website>ไอ เอ็ดดูเคชั่น โซน</website> <sort>155</sort> <uniqueip>1288</uniqueip> <uniquesession>3763</uniquesession> <pageviews>5687</pageviews> </member> <member> <url>www.ibscenter.net</url> <website>ไอบีเอส พอร์ทัลเว็บ = โลกใบใหม่ของวัยเรียน </website> <sort>214</sort> <uniqueip>851</uniqueip> <uniquesession>2168</uniquesession> <pageviews>3691</pageviews> </member> <member> <url>www.sk.ac.th</url> <website>โรงเรียนสวนกุหลาบวิทยาลัย </website> <sort>232</sort> <uniqueip>759</uniqueip> <uniquesession>3803</uniquesession> <pageviews>7035</pageviews> </member> <member> <url>www.triamudom.net</url> <website>โรงเรียนเตรียมอุดมศึกษา - เตรียมบอร์ด</website> <sort>236</sort> <uniqueip>747</uniqueip> <uniquesession>1571</uniquesession> <pageviews>2054</pageviews> </member> <member> <url>158.108.36.62/SCRIPTS/KUCityWWW/KUCityWWW.exe/www</url> <website>ชุมชน ม.เกษตร</website> <sort>252</sort> <uniqueip>686</uniqueip> <uniquesession>4864</uniquesession> <pageviews>7721</pageviews> </member> </data>

10. Source of : education.xsl
    <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <ol/> <xsl:for-each select="data/member"> <li/><xsl:value-of select="url"/> <xsl:value-of select="website"/> <xsl:value-of select="sort"/> <xsl:value-of select="uniqueip"/> <xsl:value-of select="uniquesession"/> <xsl:value-of select="pageviews"/> </xsl:for-each> </xsl:template> </xsl:stylesheet>

11. Source of : education.htm
    <html><head> <script language="javascript"> var xml,xsl,formatresult; xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = false; xml.load("education.xml"); xsl = new ActiveXObject("Microsoft.XMLDOM"); xsl.async = false; xsl.load("education.xsl"); formatresult = xml.transformNode(xsl); document.write(formatresult); </script></head> <body></body></html>

12. Source of : educationt.htm
    <html><head> <script language="javascript"> var xml,xsl,formatresult; xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = false; xml.load("http://truehits.net/xml/education.xml"); xsl = new ActiveXObject("Microsoft.XMLDOM"); xsl.async = false; xsl.load("http://lampang.thcity.com/xml/education.xsl"); formatresult = xml.transformNode(xsl); document.write(formatresult); </script></head> <body></body></html>

13. Source of : structure.php
    <form action='structure.php'> <input name=file value="http://truehits.net/xml/education.xml" size=30> <input type=submit value="Show structure of XML"> </form>
    <? if (strlen($file) < 4) { $file = "employees.xml"; } $depth = array(); function startElement($parser, $name, $attrs) { global $depth; for ($i = 0; $i < $depth[$parser]; $i++) { print " "; } print "$namen"; $depth[$parser]++; } function endElement($parser, $name) { global $depth; $depth[$parser]--; } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); if (!($fp = fopen($file, "r"))) { die("could not open XML input"); } while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } xml_parser_free($xml_parser); ?>

14. Source of : readxml.php
    <form action='readxml.php'> <input name=file value="http://truehits.net/xml/education.xml" size=30> <input type=submit value="Read XML file"> </form>
    <? if (strlen($file) < 4) { $file = "employees.xml"; } function trustedFile($file) { // only trust local files owned by ourselves if (!eregi("^([a-z]+)://", $file) && fileowner($file) == getmyuid()) { return true; } return false; } function startElement($parser, $name, $attribs) { print "<font color="#0000cc">$name</font>"; if (sizeof($attribs)) { while (list($k, $v) = each($attribs)) { print " <font color="#009900">$k</font>="<font color="#990000">$v</font>""; } } print ">"; } function endElement($parser, $name) { print "<font color=#0000cc>$name</font>"; } function characterData($parser, $data) { $data = str_replace("&",'&',$data); print "$data"; } function PIHandler($parser, $target, $data) { switch (strtolower($target)) { case "php": global $parser_file; // If the parsed document is "trusted", we say it is safe // to execute PHP code inside it. If not, display the code // instead. if (trustedFile($parser_file[$parser])) { eval($data); } else { printf("Untrusted PHP code: <i>%s</i>", htmlspecialchars($data)); } break; } } function defaultHandler($parser, $data) { if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") { printf('<font color="#aa00aa">%s</font>', htmlspecialchars($data)); } else { printf('<font size="-1">%s</font>', htmlspecialchars($data)); } } function externalEntityRefHandler($parser, $openEntityNames, $base, $systemId, $publicId) { if ($systemId) { if (!list($parser, $fp) = new_xml_parser($systemId)) { printf("Could not open entity %s at %sn", $openEntityNames, $systemId); return false; } while ($data = fread($fp, 4096)) { if (!xml_parse($parser, $data, feof($fp))) { printf("XML error: %s at line %d while parsing entity %sn", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser), $openEntityNames); xml_parser_free($parser); return false; } } xml_parser_free($parser); return true; } return false; } function new_xml_parser($file) { global $parser_file; $xml_parser = xml_parser_create(); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); xml_set_processing_instruction_handler($xml_parser, "PIHandler"); xml_set_default_handler($xml_parser, "defaultHandler"); xml_set_external_entity_ref_handler($xml_parser, "externalEntityRefHandler"); if (!($fp = @fopen($file, "r"))) { return false; } if (!is_array($parser_file)) { settype($parser_file, "array"); } $parser_file[$xml_parser] = $file; return array($xml_parser, $fp); } if (!(list($xml_parser, $fp) = new_xml_parser($file))) { die("could not open XML input"); } print "<pre>"; while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %dn", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } print "</pre>"; xml_parser_free($xml_parser); ?>

15. Source of : xmldata.php
    Data from $file
    "; # = = = function startElement($parser, $name, $attribs) { global $cc,$stat,$f,$flast,$r; if ($cc == 1) { $cc = $cc + 1; } else { $k = $cc - 1; if ($flast == "z") { $f[$k] = $name; $cc = $cc + 1; } if ($f[1] == $name && $cc > 3) { $flast = $name; } } } function endElement($parser, $name) { global $flast,$f,$r,$c,$cntc; if ($name == $f[1]) { $r = $r + 1; $cntc = $c - 1; $c = 1; } } function characterData($parser, $data) { global $d,$r,$c; if (strlen($data) > 1) { $d[$r][$c] = $data; $c = $c + 1; } } function new_xml_parser($file) { global $f01,$f02,$f03,$f04,$f05,$f06,$f07,$f08,$f09,$fcount,$flast,$parser_file,$stat; $xml_parser = xml_parser_create(); xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); if (!($fp = @fopen($file, "r"))) { return false; } if (!is_array($parser_file)) { settype($parser_file, "array"); } $parser_file[$xml_parser] = $file; return array($xml_parser, $fp); } if (!(list($xml_parser, $fp) = new_xml_parser($file))) { die("could not open XML input"); } while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %dn", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } $cc = $cc - 3; $c = $cntc; $r = $r - 1; echo "[ $r $cc ]"; print "<table><tr bgcolor=#ffffdd>"; for ($i=$cc - $cntc + 1;$i<=$cc;$i++) { echo "<td>$f[$i]</td>"; } print "</tr>"; for ($i=1;$i<=$r;$i++) { echo "<tr>"; for ($j=1;$j<=$c;$j++) { echo "<td bgcolor=#ddffff>".$d[$i][$j]."</td>"; } echo "</tr>"; } print "</table>"; xml_parser_free($xml_parser); ?>

16. Source of : xmljavas.htm
    ตัวอย่างแฟ้ม dataj.xml <?xml version="1.0"?> <information> <fld1>burin rujjanapan</fld1> <fld2>Tel. 0-1992-7223</fld2> <fld3>M</fld3> </information>
    <body> แสดงการอ่านข้อมูลจาก xmljava<br> <script language="JavaScript"> var xmlDoc; xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; xmlDoc.load("dataj.xml"); var objNode; objNode = xmlDoc.documentElement.childNodes; for (i=0;i<objNode.length;i++) document.write(objNode.item(i).firstChild.nodeValue+"<br>"); </script> </body>

17. Source of : xmledu.htm
    <body> <script language="JavaScript"> var xmlDoc; xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; xmlDoc.load("http://truehits.net/xml/education.xml"); var objNode; objNode = xmlDoc.documentElement.childNodes; for (i=0;i<objNode.length;i++) { document.write("<b>"+ objNode.item(i).firstChild.nodeName + "</b> = "); document.write(objNode.item(i).firstChild.text+" | "); document.write("<b>"+ objNode.item(i).firstChild.nextSibling.nodeName + "</b> = "); document.write(objNode.item(i).firstChild.nextSibling.text+" | "); document.write("<b>"+ objNode.item(i).firstChild.nextSibling.nextSibling.nodeName + "</b> = "); document.write(objNode.item(i).firstChild.nextSibling.nextSibling.text+" | "); document.write("<b>"+ objNode.item(i).lastChild.nodeName + "</b> = "); document.write(objNode.item(i).lastChild.text+" | "); document.write("<br>"); } </script> </body>

18. Source of : crtxml.php
    <? if (!isset($_POST['file0'])) { print "<body><pre><form action=crtxml.php method=post> File0 :: <input name=file0 value=filex> Rec0 :: <input name=rec0 value=recx> Fld1 :: <input name=fld1 value=id> Fld2 :: <input name=fld2 value=name> Fld3 :: <input name=fld3 value=salary> Fld4 :: <input name=fld4> Fld5 :: <input name=fld5> Fld6 :: <input name=fld6> Fld7 :: <input name=fld7> Fld8 :: <input name=fld8> Fld9 :: <input name=fld9> <textarea name=data rows=10 cols=80> 101,สมปอง,5000 102,บุรินทร์,25000 103,ศิรา,12000 </textarea> <input type=submit value=create_XML></form>"; } else { print '<?xml version="1.0" encoding="windows-874"?>'; print "\n<" . $_POST['file0'] . ">\n"; $getline = preg_split("/[sn]+/",$_POST['data']); for ($j=0;$j<count($getline)-1;$j++) { if (strlen($getline[$j]) > 0) { print "<". $_POST['rec0'] . ">\n"; $rec = preg_split("/[s,]+/",$getline[$j]); for ($k=0;$k<count($rec)-1;$k++) { print "<". $_POST['fld' . ($k + 1)] .">$rec[$k]</". $_POST['fld' . ($k + 1)] . ">\n"; } print "</" . $_POST['rec0'] . ">\n"; } } print "</". $_POST['file0'] . ">"; } ?>


แนะนำเว็บใหม่ : ผลการจัดอันดับ
รักลำปาง : thcity.com : korattown.com : topsiam.com : มหาวิทยาลัยโยนก
ศูนย์สอบ : รวมบทความ : ไอทีในชีวิตประจำวัน : ดาวน์โหลด : yourname@thaiall.com
ติดต่อ ทีมงาน ชาวลำปาง มีฝันเพื่อการศึกษา Tel.08-1992-7223