<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jakk's blog</title>
	<atom:link href="http://lab.araigun.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://lab.araigun.com</link>
	<description>Keep mine in this blog.</description>
	<lastBuildDate>Sun, 07 Jun 2009 13:16:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sprite xml by php script</title>
		<link>http://lab.araigun.com/2009/06/07/sprite-xml-by-php-script/</link>
		<comments>http://lab.araigun.com/2009/06/07/sprite-xml-by-php-script/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 21:10:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://lab.araigun.com/?p=124</guid>
		<description><![CDATA[ไม่ได้ update เป็นเดือนเพราะว่าไปบวชมาน่ะครับและก็ไม่รู้จะเขียนอะไรดี วันนี้เลยไปเอา php script มาแจก
พอดีวันหนึ่งต้องเขียน script อ่าน xml ขนาดประมาณ 100 MB นั้งกลุ้มอยู่นาน เลยไป search จาก google
ไปเจอ PHP script สำหรับ sprite file XML จาก http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/
แต่พอลองเอามาใช้มัน error เลยแก้ดูพอไม่มี error แล้วลองรันดู result ยังใช้ไม่ได้อีกก็เลย modify ซะเลย
ลองเอาไปใช้กันดู
$chunksize) {//when recordnum more then chunksize do end file
			preg_match("()", $buffer, $hit, PREG_OFFSET_CAPTURE); // find end item tag in buffer
			if ($hit){//set file point [...]]]></description>
			<content:encoded><![CDATA[<p>ไม่ได้ update เป็นเดือนเพราะว่าไปบวชมาน่ะครับและก็ไม่รู้จะเขียนอะไรดี วันนี้เลยไปเอา php script มาแจก<br />
พอดีวันหนึ่งต้องเขียน script อ่าน xml ขนาดประมาณ 100 MB นั้งกลุ้มอยู่นาน เลยไป search จาก google<br />
ไปเจอ<span id="more-124"></span> PHP script สำหรับ sprite file XML จาก http://www.techtalkpoint.com/articles/how-to-handle-large-xml-files-in-php/<br />
แต่พอลองเอามาใช้มัน error เลยแก้ดูพอไม่มี error แล้วลองรันดู result ยังใช้ไม่ได้อีกก็เลย modify ซะเลย<br />
ลองเอาไปใช้กันดู</p>
<p><code lang="php[lines]"><?php<br />
//initialize vars<br />
$begin=time(); // script start time<br />
$filenum = 1; // start chunk file number at 1<br />
$recordnum = 0; // start at record 1<br />
//file settings<br />
$basefilename = "chunks"; // the base file name for the chunks<br />
$xmlfile = "TURL0001.XML"; // the xml file name to be processed<br />
$xmldatadelimiter = "ONIXmessage"; // core data delimiter<br />
$xmlitemdelimiter = "product"; // record delimiter<br />
$chunksize = "2000"; // number of records in each chunk file<br />
$xmlheader ="<?xml version=\"1.0\" encoding=\"UTF-8\">\n";<br />
$xmlheader.="<$xmldatadelimiter>\n"; // xmlchunk file header<br />
//dirs and files<br />
$dir = ""; // path to where splits will be stored<br />
$exportfile = "$dir"."$basefilename-".str_pad($filenum, 4, "0",STR_PAD_LEFT).".xml";<br />
echo "Processing (".$dir."/$xmlfile)\n";</p>
<p>$handle = @fopen($dir."$xmlfile","r");<br />
if ($handle) {<br />
	while ($buffer = fread($handle,4096)) {<br />
		$recordnum += substr_count($buffer,"</$xmlitemdelimiter>"); //count amount of item in buffer, $recordnum: hold number of record in current chunk file</p>
<p>		if ($recordnum>$chunksize) {//when recordnum more then chunksize do end file<br />
			preg_match("(<\/$xmlitemdelimiter>)", $buffer, $hit, PREG_OFFSET_CAPTURE); // find end item tag in buffer<br />
			if ($hit){//set file point back to end item tag and remove data after end tag form buffer<br />
				$str = $hit[0][0];<br />
				$pos = (int)$hit[0][1];<br />
				$buff_length  = strlen($buffer);<br />
				$curr_pointer = ftell($handle);<br />
				$new_pointer  = $curr_pointer-$buff_length+$pos+strlen($str);<br />
				fseek($handle, (int)$new_pointer);<br />
				$buffer =  substr($buffer,0,$pos+strlen($str));<br />
			}<br />
			error_log("$buffer",3,$exportfile);//write the last piece of chunk file<br />
			error_log("</$xmldatadelimiter>",3,$exportfile);//write close data tag<br />
			$recordnum = 0; //reset counter<br />
			$filenum++;<br />
			$exportfile = "$dir"."$basefilename-".str_pad($filenum, 4, "0",STR_PAD_LEFT).".xml"; //create next chunk file<br />
			echo"Segment $filenum. Record ".($chunksize*$filenum).".\n";<br />
			error_log($xmlheader,3,$exportfile);<br />
		}else{<br />
			error_log("$buffer",3,$exportfile); //write buffer to chunk file<br />
		}<br />
	}<br />
	fclose($handle);<br />
}else{<br />
	echo"Unable to open file! (".$dir.$xmlfile.")\n";<br />
}<br />
$procend = time();<br />
echo "\n####\n";<br />
echo "Split Complete (".floor((($procend-$begin)/60))." Minutes)\n";<br />
$end = microtime(true);<br />
$time = $end - $start;<br />
print "parse time: $time seconds<br/>";<br />
?></code><br />
but this code isn&#8217;t 100% perfect because it&#8217;s to slow if I modify more.</p>
<p>source code and example xml: <a href='http://lab.araigun.com/2009/06/07/sprite-xml-by-php-script/spritexml/' rel='attachment wp-att-125'>source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lab.araigun.com/2009/06/07/sprite-xml-by-php-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Preview Image object in javascript</title>
		<link>http://lab.araigun.com/2009/03/15/preview-image-object-in-javascript/</link>
		<comments>http://lab.araigun.com/2009/03/15/preview-image-object-in-javascript/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 01:19:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://lab.araigun.com/?p=86</guid>
		<description><![CDATA[แต่ละภาพใน HTML document นั้นจะถูกแทนด้วย Image object ใน javascript สามารถเรียกใช้ object image ใน document ได้โดย

document.images[index]; //call by position in array
document.images.name; //call by name

สามารถเรียกใช้ constructor โดย:

var obj = new Image([width], [height]);


Properties ที่ใช้บ่อยๆ:


properties
description


height
set or return image&#8217;s height


width
set or return image&#8217;s width


src
set or return URL source


name
set or return image&#8217;s name


Events:


event
description


onload
execute when successful and complete to download image


onabort
execute when user [...]]]></description>
			<content:encoded><![CDATA[<p>แต่ละภาพใน HTML document นั้นจะถูกแทนด้วย Image object ใน javascript สามารถเรียกใช้ object image ใน document ได้โดย<span id="more-86"></span><br />
<code lang="javascript"><br />
document.images[index]; //call by position in array<br />
document.images.name; //call by name<br />
</code></p>
<p><strong>สามารถเรียกใช้ constructor โดย:</strong><br />
<code lang="javascript"><br />
var obj = new Image([width], [height]);<br />
</code><br />
<strong><br />
Properties ที่ใช้บ่อยๆ:</strong><br />
<table border="1" cellspacing="0" cellpadding="2">
<tr bgcolor="#fffbcc">
<th>properties</th>
<th>description</th>
</tr>
<tr>
<td>height</td>
<td>set or return image&#8217;s height</td>
</tr>
<tr>
<td>width</td>
<td>set or return image&#8217;s width</td>
</tr>
<tr>
<td>src</td>
<td>set or return URL source</td>
</tr>
<tr>
<td>name</td>
<td>set or return image&#8217;s name</td>
</tr>
</table>
<p><strong>Events:</strong><br />
<table border="1" cellspacing="0" cellpadding="2">
<tr bgcolor="#fffbcc">
<th>event</th>
<th>description</th>
</tr>
<tr>
<td>onload</td>
<td>execute when successful and complete to download image</td>
</tr>
<tr>
<td>onabort</td>
<td>execute when user abort download image</td>
</tr>
<tr>
<td>onerror</td>
<td>execute when error occur while loading image (image not found)</td>
</tr>
</table>
<p><strong>more source:</strong> <a href="http://www.w3schools.com/htmldom/dom_obj_image.asp">w3schools</a>, <a href="https://developer.mozilla.org/en/Gecko_DOM_Reference">mozilla developer center</a><br />
<strong><br />
example image preloading:</strong></p>
<p><strong>html code:</strong><code lang="html4strict"><html><br />
<head></p>
<style type="text/css">
#thumb_container img{
	margin:2px;
	cursor:pointer;
}
</style>
<p><script type="text/javascript" src="example.js"></script><br />
</head><br />
<body></p>
<table border="0"  width="630" height="430">
<tr align="center" valign="middle">
<td><img name="loading_img" src="images/animal1.jpg" /> </td>
</tr>
</table>
<table border="0"  width="630" >
<tr align="center" valign="middle">
<td>
<div id="thumb_container"><img src="images/thumb_animal1.jpg" rel="images/animal1.jpg" /><img src="images/thumb_animal2.jpg" rel="images/animal2.jpg"/><img src="images/thumb_animal3.jpg" rel="images/animal3.jpg"/></div>
</tr>
</table>
<p></body><br />
</html></code></p>
<p><strong>javascript code in <span style="color:red;">example.js</span>:</strong><code lang="javascript[lines]">function change_img(url){<br />
	document.images.loading_img.src = 'images/loading.gif';//show loading image<br />
	var img = new Image();<br />
	img.onload = function(){<br />
		document.images.loading_img.src = url;//change image when loading complete<br />
	}<br />
	img.src = url;//preload image<br />
}<br />
function onclick_thumb(){<br />
	var e = arguments[0] || window.event;//get event object, note: this fix for IE<br />
	var target_img = e.target || e.srcElement;//get target object (ie: image object), note: this fix for IE<br />
	var url = target_img.getAttribute('rel');//get url form attricute 'rel'<br />
	change_img(url);<br />
}<br />
function init_example(){<br />
	var thumb_obj = document.getElementById('thumb_container').getElementsByTagName('img');//get thumbnail image's objects<br />
	for(var i in thumb_obj){<br />
		thumb_obj[i].onclick = onclick_thumb;//add onclick event for each object<br />
	}<br />
	var img = new Image();<br />
	img.src = 'images/loading.gif';//preloading loading image<br />
}<br />
if(window.addEventListener){<br />
	window.addEventListener("load", init_example, false); //run initial fuction when complete loading object<br />
}else if(window.attachEvent){<br />
	window.attachEvent("onload",init_example);//fix run initial fuction for fucking browser(IE)<br />
} </code></p>
<p>demo <a href="http://lab.araigun.com/examples/image_object/">here</a><br />
source <a href='http://lab.araigun.com/wp-content/uploads/2009/03/sorce.zip'>here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lab.araigun.com/2009/03/15/preview-image-object-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Image Magick for image processing</title>
		<link>http://lab.araigun.com/2009/03/06/using-image-magick-for-image-processing/</link>
		<comments>http://lab.araigun.com/2009/03/06/using-image-magick-for-image-processing/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 11:17:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[image processing]]></category>

		<guid isPermaLink="false">http://lab.araigun.com/?p=71</guid>
		<description><![CDATA[Image Magick คำนี่ผมไม่ได้สะกดผิดหรอกนะ แต่มันเป็นชื่อ library ที่ใช้สำหรับ image processing เหมือนกับ GD library แต่ผมคิดว่า performance ของ Image Magick ดีกว่าและใช้จัดการ resource อย่างเช่น memory ได้ดีกว่า Image Magick สามารถใช้ได้ทั้ง command line หรือ interface สำหรับภาษาต่างๆเช่น PHP, JAVA, .NET และอื่นๆ ปัจจุบันมีถึง version 6 สามารถ download และหาดู document ได้จาก website ทางการ : http://www.imagemagick.org แต่version ที่ผมจะนำมาใช้เป็นตัวอย่างเป็น version 4 ซึ่งเป็น statically linked  binary (อันนี้ใครรู้วิธี [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Image Magick </strong>คำนี่ผมไม่ได้สะกดผิดหรอกนะ แต่มันเป็นชื่อ library ที่ใช้สำหรับ image processing เหมือนกับ GD library แต่ผมคิดว่า performance ของ Image Magick ดีกว่าและใช้จัดการ resource อย่างเช่น memory ได้ดีกว่า Image Magick สามารถใช้ได้ทั้ง command line หรือ interface สำหรับภาษาต่างๆเช่น PHP, JAVA, .NET และอื่นๆ ปัจจุบันมีถึง version 6 สามารถ download และหาดู document ได้จาก website ทางการ : <a href="http://www.imagemagick.org" target="_blank">http://www.imagemagick.org</a> <span id="more-71"></span>แต่version ที่ผมจะนำมาใช้เป็นตัวอย่างเป็น version 4 ซึ่งเป็น statically linked  binary (อันนี้ใครรู้วิธี compile สอนผมที)<br />
<strong><br />
test resize 440&#215;504 image by Image Magick</strong><br />
<strong>code here:</strong><br />
<code lang="php[lines]"><?php<br />
function resize($img, $w,  $newfilename) {<br />
	$path_exe = "convert $img -resize \"$w\" $newfilename";<br />
	exec($path_exe);<br />
}<br />
$time_start = microtime(true);<br />
$mem_before = memory_get_usage();//this function avaliable in PHP 5.2+</p>
<p>resize('alucard.jpg', 100, 'alucard_0.jpg');</p>
<p>$mem_after = memory_get_peak_usage();//this function avaliable in PHP 5.2+<br />
$time_end = microtime(true);<br />
$time = $time_end - $time_start;<br />
print '<br />
<table>
<tr align="center" valign="middle">
<td ><image src="alucard.jpg"/></td>
<td > before</td>
</tr>
<tr align="center" valign="middle">
<td ><image src="alucard_0.jpg"/></td>
<td > after</td>
</tr>
</table>
<p>';<br />
print "parse time: $time seconds<br/>";<br />
print "memory usage before resize: $mem_before<br/> memory peak while resize: $mem_after <br/> memory usage for resize:".($mem_after-$mem_before);<br />
?></code><br />
<strong>result here:</strong><br />
<img src="http://lab.araigun.com/wp-content/uploads/2009/03/m.png" alt="result" title="result" width="504" height="729" class="alignnone size-full wp-image-75" /></p>
<p><strong>test resize 440×504 image by GD lib</strong><br />
<strong>code here:</strong><br />
<code lang="php[lines]"><?<br />
function resize($img, $w, $newfilename) {</p>
<p> //Get Image size info<br />
 	$imgInfo = getimagesize($img);<br />
	$format = ereg_replace(".*\.(.*)$","\\1",$img);<br />
	$format = strtolower($format);</p>
<p> 	switch ($format) {<br />
		case "gif": 	$im = imagecreatefromgif($img); break;<br />
		case "jpg":<br />
		case "jpeg":	$im = imagecreatefromjpeg($img);  break;<br />
		case "png": 	$im = imagecreatefrompng($img); break;<br />
		default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;<br />
 	}<br />
	//calculate size<br />
	if($imgInfo[0]>=$imgInfo[1]){<br />
		$nWidth = $w;<br />
		$nHeight = ($nWidth/$imgInfo[0])*$imgInfo[1];<br />
	}else{<br />
		$nHeight = $w;<br />
		$nWidth = ($nHeight/$imgInfo[1])*$imgInfo[0];<br />
	}<br />
 	$nWidth = round($nWidth);<br />
 	$nHeight = round($nHeight);<br />
	$newImg = imagecreatetruecolor($nWidth, $nHeight);</p>
<p> // Check if this image is PNG or GIF, then set if Transparent<br />
	if(($imgInfo[2]==3)){<br />
		imagealphablending($newImg, false);<br />
		imagesavealpha($newImg,true);<br />
		$transparent = imagecolortransparent($im);<br />
		imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);<br />
	}elseif(($imgInfo[2] == 1)){<br />
		imagetruecolortopalette($newImg, false, 255);<br />
		$idx = imagecolortransparent($im);<br />
		$rgba = imagecolorsforindex($im,idx);<br />
		$transparent = imagecolorallocatealpha($newImg, $rgba['red'], $rgba['green'], $rgba['blue'],$rgba['alpha']);<br />
		imagecolortransparent($newImg, $transparent);<br />
		imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);<br />
	}<br />
 	imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);</p>
<p> //Generate the file, and rename it to $newfilename<br />
	switch ($imgInfo[2]) {<br />
		case 1: imagegif($newImg,$newfilename); break;<br />
		case 2: imagejpeg($newImg,$newfilename);  break;<br />
		case 3: imagepng($newImg,$newfilename); break;<br />
		default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;<br />
	}<br />
	imagedestroy($im);<br />
	return $newfilename;<br />
}<br />
$time_start = microtime(true);<br />
$mem_before = memory_get_usage();//this function avaliable in PHP 5.2+</p>
<p>resize('alucard.jpg', 100, 'alucard_1.jpg');</p>
<p>$mem_after = memory_get_peak_usage();//this function avaliable in PHP 5.2+<br />
$time_end = microtime(true);<br />
$time = $time_end - $time_start;<br />
print '<br />
<table>
<tr align="center" valign="middle">
<td ><image src="alucard.jpg"/></td>
<td > before</td>
</tr>
<tr align="center" valign="middle">
<td ><image src="alucard_1.jpg"/></td>
<td > after</td>
</tr>
</table>
<p>';<br />
print "parse time: $time seconds<br/>";<br />
print "memory usage before resize: $mem_before<br/> memory peak while resize: $mem_after <br/> memory usage for resize:".($mem_after-$mem_before);<br />
?></code><br />
<strong>result here:</strong><img src="http://lab.araigun.com/wp-content/uploads/2009/03/g.png" alt="result" title="result" width="497" height="711" class="alignnone size-full wp-image-77" /></p>
<p><strong>source code including  statically linked Image Magick V4: </strong><a href='http://lab.araigun.com/wp-content/uploads/2009/03/test.zip'>source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lab.araigun.com/2009/03/06/using-image-magick-for-image-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Xpath</title>
		<link>http://lab.araigun.com/2009/03/04/using-xpath/</link>
		<comments>http://lab.araigun.com/2009/03/04/using-xpath/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 05:14:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[Xpath]]></category>

		<guid isPermaLink="false">http://lab.araigun.com/?p=50</guid>
		<description><![CDATA[Xpath เป็นภาษาที่ใช้ในการเลือก node ที่สนใจจาก XML ซึ่งสะดวกกว่าการใช้การใช้ DOM method ในการเข้าถึง nodeเนื่องจากสามารถเข้าถึง node ที่ต้องการได้ด้วยเพียงคำสั่งเดียว

ตัวอย่าง selector ที่ใช้บ่อยๆได้แก่


selector
description


nodename
select all child form nodename


/
select leaf node node form root


//
select all node node form current node


@
select attributes


[expr]
user expression find in specific node


sample xml:


   
      Gambardella, Matthew
      Computer
44.95
2000-10-01
   
 [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Xpath</strong> เป็นภาษาที่ใช้ในการเลือก node ที่สนใจจาก XML ซึ่งสะดวกกว่าการใช้การใช้ DOM method ในการเข้าถึง nodeเนื่องจากสามารถเข้าถึง node ที่ต้องการได้ด้วยเพียงคำสั่งเดียว<br />
<span id="more-50"></span><br />
<strong>ตัวอย่าง selector ที่ใช้บ่อยๆได้แก่</strong></p>
<table border="1" cellspacing="0" cellpadding="2">
<tr bgcolor="#fffbcc">
<th>selector</th>
<th>description</th>
</tr>
<tr>
<td>nodename</td>
<td>select all child form nodename</td>
</tr>
<tr>
<td>/</td>
<td>select leaf node node form root</td>
</tr>
<tr>
<td>//</td>
<td>select all node node form current node</td>
</tr>
<tr>
<td>@</td>
<td>select attributes</td>
</tr>
<tr>
<td>[expr]</td>
<td>user expression find in specific node</td>
</tr>
</table>
<p><strong>sample xml:</strong><br />
<code lang="xml[lines]"><?xml version="1.0" encoding="utf-8"?><br />
<catalog><br />
   <book id="bk101"><br />
      <author>Gambardella, Matthew</author></p>
<p>      <genre>Computer</genre></p>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
   </book><br />
   <book id="bk102"><br />
      <author>Ralls, Kim</author></p>
<p>      <genre>Fantasy</genre></p>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies,<br />
      an evil sorceress, and her own childhood to become queen<br />
      of the world.</description><br />
   </book><br />
   <book id="bk103"><br />
      <author>Corets, Eva</author></p>
<p>      <genre>Fantasy</genre></p>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology<br />
      society in England, the young survivors lay the<br />
      foundation for a new society.</description><br />
   </book><br />
   <book id="bk104"><br />
      <author>Corets, Eva</author></p>
<p>      <genre>Fantasy</genre></p>
<price>5.95</price>
<publish_date>2001-03-10</publish_date>
   </book><br />
   <book id="bk105"><br />
      <author>Corets, Eva</author></p>
<p>      <genre>Fantasy</genre></p>
<price>5.95</price>
<publish_date>2001-09-10</publish_date>
   </book><br />
   <book id="bk106"><br />
      <author>Randall, Cynthia</author></p>
<p>      <genre>Romance</genre></p>
<price>4.95</price>
<publish_date>2000-09-02</publish_date>
      <description>When Carla meets Paul at an ornithology<br />
      conference, tempers fly as feathers get ruffled.</description><br />
   </book><br />
   <book id="bk107"><br />
      <author>Thurman, Paula</author></p>
<p>      <genre>Romance</genre></p>
<price>4.95</price>
<publish_date>2000-11-02</publish_date>
   </book><br />
   <book id="bk108"><br />
      <author>Knorr, Stefan</author></p>
<p>      <genre>Horror</genre></p>
<price>4.95</price>
<publish_date>2000-12-06</publish_date>
   </book><br />
   <book id="bk109"><br />
      <author>Kress, Peter</author></p>
<p>      <genre>Science Fiction</genre></p>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
   </book><br />
   <book id="bk110"><br />
      <author>O'Brien, Tim</author></p>
<p>      <genre>Computer</genre></p>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
   </book><br />
   <book id="bk111"><br />
      <author>O'Brien, Tim</author></p>
<p>      <genre>Computer</genre></p>
<price>36.95</price>
<publish_date>2000-12-01</publish_date>
   </book><br />
   <book id="bk112"><br />
      <author>Galos, Mike</author></p>
<p>      <genre>Computer</genre></p>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
   </book><br />
</catalog></code></p>
<p><strong>sample to use Xpath by php:</strong><br />
<code lang="php[lines]"><?php<br />
function getSelectXpathValue(&#038;$xpath,$expression){<br />
	$obj = $xpath->xpath_eval($expression);<br />
	$value = array();<br />
	if(@count($obj->nodeset)){<br />
		foreach($obj->nodeset as $node){<br />
			$value[] = $node->get_content();<br />
		}<br />
	}<br />
	return $value;<br />
}</p>
<p>$xml = domxml_open_file('books.xml');<br />
$xpath = &#038;$xml->xpath_new_context();</p>
<p>$value = getSelectXpathValue($xpath, "/catalog/book[@id='bk102']");<br />
print_r($value);<br />
/* output is:<br />
Array<br />
(<br />
    [0] =><br />
      Ralls, Kim<br />
      Midnight Rain<br />
      Fantasy<br />
      5.95<br />
      2000-12-16<br />
      A former architect battles corporate zombies,<br />
      an evil sorceress, and her own childhood to become queen<br />
      of the world.</p>
<p>)<br />
*/<br />
$value = getSelectXpathValue($xpath, "/catalog/book[genre='Fantasy'][description]/title");<br />
print_r($value);<br />
/* output is:<br />
Array<br />
(<br />
    [0] => Midnight Rain<br />
    [1] => Maeve Ascendant<br />
)<br />
*/<br />
$value = getSelectXpathValue($xpath, "//title");<br />
print_r($value);<br />
/* output is:<br />
Array<br />
(<br />
    [0] => XML Developer's Guide<br />
    [1] => Midnight Rain<br />
    [2] => Maeve Ascendant<br />
    [3] => Oberon's Legacy<br />
    [4] => The Sundered Grail<br />
    [5] => Lover Birds<br />
    [6] => Splish Splash<br />
    [7] => Creepy Crawlies<br />
    [8] => Paradox Lost<br />
    [9] => Microsoft .NET: The Programming Bible<br />
    [10] => MSXML3: A Comprehensive Guide<br />
    [11] => Visual Studio 7: A Comprehensive Guide<br />
)<br />
*/<br />
?></code></p>
]]></content:encoded>
			<wfw:commentRss>http://lab.araigun.com/2009/03/04/using-xpath/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DOM &amp; SAX</title>
		<link>http://lab.araigun.com/2009/03/02/dom-sax/</link>
		<comments>http://lab.araigun.com/2009/03/02/dom-sax/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 04:32:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[SAX]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://lab.araigun.com/?p=16</guid>
		<description><![CDATA[หลายคนที่เคยโปรแกรมเกี่ยวกับขู้อมูลที่เป็นลักษณะ XML คงจะรู้จัก DOM กันมาบ้างแล้ว
DOM (Document Object Model) คือวิธีจัดการกับ XML อย่างหนึ่งโดย แทน node ต่างๆใน file ด้วย object ทำให้การเข้าถึง node ต่างๆทำได้ง่ายขึ้น โดยการใช้ method ของ object แต่การจะสร้าง object จาก XML จะต้อง parse XML ทั้งหมดลง memory ทำให้วิธีนี้ไม่เหมาะที่จะใช้กับ file XML ที่มีขนาดใหญ่ ถ้าไม่เชื่อลองได้โดยเอา XML ขนาด 10 MB ไปเปิดกับ browser อย่าง IE หรือ firefox ดูเพราะ browser ทั่วไปใช้ DOM จัดการกับ file ลักษณะ [...]]]></description>
			<content:encoded><![CDATA[<p>หลายคนที่เคยโปรแกรมเกี่ยวกับขู้อมูลที่เป็นลักษณะ XML คงจะรู้จัก DOM กันมาบ้างแล้ว</p>
<p><strong>DOM (Document Object Model) </strong>คือวิธีจัดการกับ XML อย่างหนึ่งโดย แทน node ต่างๆใน file ด้วย object ทำให้การเข้าถึง node ต่างๆทำได้ง่ายขึ้น โดยการใช้ method ของ object แต่การจะสร้าง object จาก XML จะต้อง parse XML ทั้งหมดลง memory ทำให้วิธีนี้ไม่เหมาะที่จะใช้กับ file XML ที่มีขนาดใหญ่ ถ้าไม่เชื่อลองได้โดยเอา XML ขนาด 10 MB ไปเปิดกับ browser อย่าง IE หรือ firefox ดูเพราะ browser ทั่วไปใช้ DOM จัดการกับ file ลักษณะ XML อยู่แล้ว ฉะนั้นจึงมีอีกวิธีหนึ่งที่จัดการกับ XML นั้นก็คือ SAX<br />
<span id="more-16"></span><br />
<strong><br />
SAX (Simple API for XML)</strong> เป็นวิธีการจัดการ XML โดย access แต่ละ node ไปตามลำดับ จนจบ file โดยจะใช้ event จัดการกับแต่ละ node event ของ SAX ประกอบไปด้วย
<ul>
<li>Processing Instruction (ส่วนที่ใช้ describe XML: <?xml version="1.0" encoding="UTF-8"?> )</li>
<li>Element nodes (แบ่งเป็น 2 eventคือ start element, end element)</li>
<li>Text nodes</li>
<li>Comments</li>
</ul>
<p>ตัวอย่างการใช้ SAX กับ php</p>
<p><strong>Sample xml:</strong><a href='http://lab.araigun.com/wp-content/uploads/2009/03/books.xml'>books.xml</a><br />
<strong>sample php code:</strong><br />
<code lang="php[lines]"><?php<br />
class BookSax{<br />
	var $current_data = array();<br />
	function start_element($parser, $name, $attrs){<br />
		//checking interest node and setting need data<br />
		if($name == 'book'){<br />
			$this->current_data['id'] = $attrs['id'];<br />
		}<br />
		if($name == 'title'){<br />
			$this->get_data = 'title';<br />
		}<br />
		if($name == 'price'){<br />
			$this->get_data = 'price';<br />
		}<br />
	}<br />
	function end_element($parser, $name){<br />
		//print data when get all boook's data<br />
		if($name == 'book'){<br />
			print '<strong>ID:</strong> '.$this->current_data['id']<br />
				.' <strong>TITLE:</strong> '.$this->current_data['title']<br />
				.' <strong>PRICE:</strong> '.$this->current_data['price'].'<br/>';<br />
			//clear previous boook's data<br />
			$this->current_data = array();<br />
		}<br />
	}<br />
	function text($parser, $text){<br />
		//collecting need data<br />
		if(!empty($this->get_data)){<br />
			$this->current_data[$this->get_data] = $text;<br />
			$this->get_data = NULL;<br />
		}<br />
	}<br />
}</p>
<p>//create parser<br />
$book_sax = new BookSax();<br />
$parser = xml_parser_create();</p>
<p>//set parser's option<br />
xml_set_object ($parser, $book_sax);<br />
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);</p>
<p>//set call back<br />
xml_set_element_handler($parser,"start_element","end_element");<br />
xml_set_character_data_handler($parser, "text");</p>
<p>if($fp = fopen('books.xml', "r")){<br />
		//read xml file<br />
	 while ($data = fread($fp, 4096)){<br />
		 $this_chunk_parsed = xml_parse($parser, $data, feof($fp));//do parse data and excute callback<br />
		 if (!$this_chunk_parsed) {<br />
				 $error_code = xml_get_error_code($parser);<br />
				 $error_text = xml_error_string($error_code);<br />
				 $error_line = xml_get_current_line_number($parser);<br />
				 $output_text = "Parsing problem at line $error_line: $error_text";<br />
				 die($output_text);<br />
		 }<br />
	 }<br />
}<br />
fclose($fp);<br />
xml_parser_free($parser);<br />
?></code></p>
<p><strong>result here:</strong><br />
<img src="http://lab.araigun.com/wp-content/uploads/2009/03/result.png" alt="result" title="result" width="497" height="392" class="alignnone size-full wp-image-24" /></p>
<p><strong>code here:</strong> <a href='http://lab.araigun.com/wp-content/uploads/2009/03/source.zip'>source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lab.araigun.com/2009/03/02/dom-sax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://lab.araigun.com/2009/03/01/hello-world/</link>
		<comments>http://lab.araigun.com/2009/03/01/hello-world/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 16:25:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://lab.araigun.com/?p=1</guid>
		<description><![CDATA[ทดสอบ format code
if($this->comment == 1){
  print 'Hello world!';
}
]]></description>
			<content:encoded><![CDATA[<p>ทดสอบ format code<br />
<code lang="php[lines]">if($this->comment == 1){<br />
  print 'Hello world!';<br />
}</code></p>
]]></content:encoded>
			<wfw:commentRss>http://lab.araigun.com/2009/03/01/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
