สารบัญ
Section 1 : How to write XML file
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)
list01.xsl (ใช้ข้อมูลจาก employees.xml) [ดู source code ]
:: กำหนดรูปแบบ extensible stylesheet language เพื่อให้โปรแกรมอื่น ๆ เรียกไปใช้ และกำหนดรูปแบบได้ตามที่กำหนด ในที่นี้จะถูกเรียกใช้โดย list01.htm ผลที่ได้จะเป็นข้อมูลธรรมดาที่ไม่ถูกจัดเรียก และท่านสามารถทดสอบโปรแกรมเพื่อดูผลได้จาก list01.htm
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)
list02.xsl (ใช้ข้อมูลจาก employees.xml) [ดู source code ]
list02.htm (ใช้ข้อมูลจาก employees.xml) [ดู source code ]
:: ทดสอบที่ [thaiall.com และ thcity.com ]
Section 4 : Using of XSL and HTM (Case 3)
list03sale.xsl (ใช้ข้อมูลจาก employees.xml) [ดู source code ]
:: เลือกเฉพาะข้อมูลที่มี department = sale
list03admin.xsl (ใช้ข้อมูลจาก employees.xml) [ดู source code ]
:: เลือกเฉพาะข้อมูลที่มี department = admin
list03.htm (ใช้ข้อมูลจาก employees.xml) [ดู source code ]
:: ทดสอบที่ [thaiall.com และ thcity.com ]
:: มีการใช้ความสามารถของ java script เพื่อส่งความต้องการจาก form ไปเลือก xsl ที่ต้องการ
Section 5 : Using of XSL and HTM (Case 4)
Data file : http://truehits.net/xml/education.xml [ดู source code ]
:: คัดลอกแฟ้มนี้มาจาก truehits.net เมื่อวันที่ 3 กันยายน 2545
education.xsl (ใช้ข้อมูลจาก education.xml) [ดู source code ]
education.htm (ใช้ข้อมูลจาก education.xml) [ดู source code ]
:: ทดสอบที่ [thaiall.com และ thcity.com ]
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
structure.php (ใช้ข้อมูลจาก http://truehits.net/xml/education.xml หรือท่านกำหนดขึ้นเองได้) [ดู source code ]
:: ทดสอบที่ [thcity.com ] แต่ thaiall.com ใช้ structure.php แล้วอ่าน XML ไม่ได้ ปัญหาน่าจะมามาก server
readxml.php (ใช้ข้อมูลจาก http://truehits.net/xml/education.xml หรือท่านกำหนดขึ้นเองได้) [ดู source code ]
:: ทดสอบที่ [thcity.com ] แต่ thaiall.com ใช้ readxml.php แล้วอ่าน XML ไม่ได้ ปัญหาน่าจะมามาก server
xmldata.php (ใช้ข้อมูลจาก employees.xml หรือท่านกำหนดขึ้นเองได้) [ดู source code ]
:: ทดสอบที่ [thcity.com ] แต่ thaiall.com ใช้ xmldata.php แล้วอ่าน XML ไม่ได้ ปัญหาน่าจะมามาก server
:: โปรแกรมนี้ถูกปรับปรุงให้สามารถอ่าน xml ได้โดยไม่ต้องทราบโครงสร้างของแฟ้มก่อน แต่อ่านมาแสดงผลในตารางได้ ได้ทดสอบกับหลาย ๆ แล้วของ xml แต่อาจมีปัญหากับบางแฟ้มที่มีโครงสร้างซับซ้อนได้ ... ต้องลองดูครับ
Section 7 : javascript in HTML
xmljavas.htm (ใช้ข้อมูลจาก dataj.xml หรือท่านกำหนดขึ้นเองได้) [ดู source code ]
:: ทดสอบที่ [thaiall.com และ thcity.com ] ผลการทดสอบมีปัญหาเรื่อง security ให้ดูจากข้อ 12
:: แฟ้ม XML บางแฟ้มมีระเบียนเดียว โปรแกรม Javascript นี้จะอ่านข้อมูลทั้งหมดไปใช้ได้ โดยไม่คำนึงว่ามีกี่ระเบียน มักเป็นข้อมูลที่เก็บข้อกำหนดต่าง ๆ เพียงระเบียนเดียวเท่านั้น ถ้ามีหลายระเบียนก็จะใช้หลักการในหัวข้อต่อไป
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
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
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
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 "<$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
if (strlen($file) < 4) { $file = "employees.xml"; }
$cntc = 0; # ใช้นับ column จริงจากข้อมูล
$r = 1; # ใช้นับ record
$c = 1; # ใช้นับ column แต่ละระเบียน จึง reset
$cc = 1; # ใช้นับจำนวน column ตอนอ่านชื่อ column
$flast = "z"; #
echo " 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 "$f[$i] "; }
print " ";
for ($i=1;$i<=$r;$i++) {
echo "";
for ($j=1;$j<=$c;$j++) { echo "".$d[$i][$j]." "; }
echo " ";
}
print "
";
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
if (!isset($_POST['file0'])) {
print " ";
} 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]". $_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
")
if(url == "www.yonok.a") document.write("")
if(url == "www.thaiall") document.write("")
if(url == "www.perlphp") document.write("")
if(url == "thaiall.kor") document.write("")
-->