เอ็กเอ็มแอล (XML : eXtensible Markup Language)
เว็บเพจสำรอง (Backup Webpages) : thaiall.com | lampang.thcity.com
ปรับปรุง : 2551-02-02 (ปรับปรุง 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
    45001 thanom 15000 45002 pikun 9999 45003 sombat 12500

2. Source of : list01.xsl

3. Source of : list01.htm

4. Source of : list02.xsl
    , , ,

5. Source of : list02.htm

6. Source of : list03sale.xsl
    , , ,

7. Source of : list03admin.xsl
    , ,

8. Source of : list03.htm

9. Data file : http://truehits.net/xml/education.xml
    ]> www.friends.co.th Friends.co.th - เพื่อนเก่า ทะเบียนรุ่น ทุกสถาบัน 10 14701 50974 76436 www.ru.ac.th มหาวิทยาลัยรามคำแหง 68 3068 4698 4976 www.stou.ac.th มหาวิทยาลัยสุโขทัยธรรมาธิราช 74 2905 8621 13482 www.thaiall.com thaiall.com,สอบ,สอน,สืบค้น,บทความ,ภาพสวย,จัดอันดับ 114 1906 5054 8201 www.NetDesign.ac.th NetDesign ศูนย์อบรมด้าน Web Design และ E-Commerce อันดับ1 ของเมืองไทย 138 1504 4614 6769 www.eduzones.com ไอ เอ็ดดูเคชั่น โซน 155 1288 3763 5687 www.ibscenter.net ไอบีเอส พอร์ทัลเว็บ = โลกใบใหม่ของวัยเรียน 214 851 2168 3691 www.sk.ac.th โรงเรียนสวนกุหลาบวิทยาลัย 232 759 3803 7035 www.triamudom.net โรงเรียนเตรียมอุดมศึกษา - เตรียมบอร์ด 236 747 1571 2054 158.108.36.62/SCRIPTS/KUCityWWW/KUCityWWW.exe/www ชุมชน ม.เกษตร 252 686 4864 7721

10. Source of : education.xsl

11. Source of : education.htm

12. Source of : educationt.htm

13. Source of : structure.php

14. Source of : readxml.php
    $name
    "; if (sizeof($attribs)) { while (list($k, $v) = each($attribs)) { print " $k="$v""; } } print ">"; } function endElement($parser, $name) { print "$name
    >n"; } 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: %s", htmlspecialchars($data)); } break; } } function defaultHandler($parser, $data) { if (substr($data, 0, 1) == "&" && substr($data, -1, 1) == ";") { printf('%s', htmlspecialchars($data)); } else { printf('%s', 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 "
    ";
    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 "
    "; 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 ""; for ($i=$cc - $cntc + 1;$i<=$cc;$i++) { echo ""; } print ""; for ($i=1;$i<=$r;$i++) { echo ""; for ($j=1;$j<=$c;$j++) { echo ""; } echo ""; } print "
    $f[$i]
    ".$d[$i][$j]."
    "; xml_parser_free($xml_parser); ?>

16. Source of : xmljavas.htm
    ตัวอย่างแฟ้ม dataj.xml burin rujjanapan Tel. 0-1992-7223 M
    แสดงการอ่านข้อมูลจาก xmljava

17. Source of : xmledu.htm

18. Source of : crtxml.php
    File0 :: Rec0 :: Fld1 :: Fld2 :: Fld3 :: Fld4 :: Fld5 :: Fld6 :: Fld7 :: Fld8 :: Fld9 ::
    "; } else { print ''; print "n<" . $_POST['file0'] . ">n"; $getline = preg_split("/[sn]+/",$_POST['data']); for ($j=0;$j 0) { print "<". $_POST['rec0'] . ">n"; $rec = preg_split("/[s,]+/",$getline[$j]); for ($k=0;$k$rec[$k]n"; } print "n"; } } print ""; } ?>


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