[X] Choose Font Here

SimpleXML in PHP


Joomla မွာ RSS ကိုထည့္ရင္းနဲ႕ PHP မွာ XML ကိုသံုးျပီး Web service လုပ္တာကိုပါဆက္ျပီးဖတ္ျဖစ္သြားပါတယ္။ ဒီ post မွာကေတာ့

XML တစ္ခုဘယ္လို create လုပ္မယ္ ဆိုတာပါမယ္။ ျပီးေတာ့ XML ထဲက Data ေတြကိုဘယ္လို ဖတ္တယ္ ဆိုတာေလးကို

ေရးမွာျဖစ္ပါတယ္။ Comment မွာ XML အေၾကာင္းသိခ်င္တယ္ေျပာတဲ့သူ အတြက္လည္းဒီ post ကိုလာဖတ္ေစခ်င္ ပါတယ္။

ျပီးေတာ့မွေနာက္တစ္ဆင့္အေနနဲ႕ Web service ေတြကိုသြားပါမယ္။ အခုေတာ့ very easy and simple ျဖစ္တာေလးနဲ႕ပဲစလိုက္ပါမယ္။

<?
$xmlstr = <<<XML
<books>
<book>
<title/>
</book>
</books>
XML;

$xml = new SimpleXMLElement($xmlstr);

$book = $xml->book[0];
$book->addAttribute('type', 'Computer');

$book->title = 'PHP Web Services';

$author = $xml->book[0]->addChild('author');
$author->addChild('name', 'Zack');

file_put_contents('book.xml', $xml->asXML());
?>


ဒီ code မွာၾကည့္မယ္ဆိုရင္ေတာ့
SimpleXMLElement ဆိုတာကိုေတြ႕နိုင္ပါတယ္။ SimpleXMLElement  ကိုသံုးျပီး XML object

တစ္ခုကိုေဆာက္လိုက္တာျဖစ္ပါတယ္။

 

$xml = new SimpleXMLElement($xmlstr);

 

အဲဒါဆိုရင္ XML object တစ္ခုကိုရျပီ။ XML ေတြရဲ႕ structure အရ Book ဆိုတဲ့ element တစ္ခုကို create လုပ္မယ္ဗ်ာ။

 

$book = $xml->book[0];


 

Book[0] ဆိုတာကေတာ့ array notation ထဲကေန index 0 ကိုယူလိုက္တာျဖစ္ပါတယ္။ XML ထဲမွာနာမည္တူ element

ေတြကတစ္ခုထက္မကရွိေနနိုင္တာမို႕ first element ကိုယူမယ္လို႕ဆိုလိုတာျဖစ္ပါတယ္။

 

$book->addAttribute('type', 'Computer');


 

ဒါကေတာ့ Book ဆိုတဲ့ Element ထဲကို type ဆိုတဲ့ attribute တစ္ခုကိုထည့္လိုက္ျဖစ္ပါတယ္။

 

$book->title = 'PHP Web Services';


 

ဒီ line ကိုရွင္းရမယ္ဆိုရင္ေတာ့ကြ်န္ေတာ္တို႕ XML object ကို $xmlstrဆိုတဲ့ string ေလးကို pass လုပ္ခဲ့တာကို သတိျပဳမိမယ္

ထင္ပါတယ္။ အဲဒီ string ထဲမွာ <title/> ဆိုတာဟာဒီ $book->title ဆိုျပီး value တစ္ခုထည့္ဖို႕လုပ္ခဲ့တာျဖစ္ပါတယ္။

Book ဆိုတဲ့ Element ထဲမွာ Author ဆိုတဲ့ Element တစ္ခုကို create လုပ္တယ္။ ျပီးေတာ့လည္း Name ဆိုတဲ့ Author ရဲ႕

Child element ကိုထပ္ျပီး Dynamically လုပ္မယ္ဗ်ာ။ အဲဒီအတြက္သံုးထားတာကေတာ့ addChild ဆိုတဲ့ Function ပဲျဖစ္ပါတယ္။

ဒီလိုဆိုရင္ေတာ့ author ဆိုတဲ့ element တစ္ခုရျပီး

 

$author = $xml->book[0]->addChild('author');


 

author ထဲကို child element တစ္ခုထည့္ခ်င္တယ္ဆိုရင္ေတာ့

 

$author->addChild('name', 'Zack');

 

ေနာက္ဆံုးအဆင့္အေနနဲ႕ကေတာ့ file_put_contents ဆိုတဲ့ method ကိုသံုးျပီး XML file တစ္ခုကို create လုပ္သြားမွာျဖစ္ပါတယ္။

 

file_put_contents('book.xml', $xml->asXML());


 

asXML ဆိုတဲ့ function ကိုသံုးျပီး simpleXML Element ထဲက string ကိုယူျပီး file_put_contents ကိုသံုးျပီး book.XML

ကို႐ိုက္္လိုက္တာျဖစ္ပါတယ္။ ႐ိုက္လိုက္တဲ့ XML ကိုဖြင့္ၾကည့္လိုက္မယ္ဆိုရင္ေတာ့ဒီလိုျမင္ရပါလိမ့္မယ္။

 

<?xml version="1.0"?>
<books>
<book type="Computer">
<title>PHP Web Services</title>
<author><name>Zack</name></author></book>
</books>



Comments (0)
Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.

Login Form

Categories Table View

JoomlaWatch Stats 1.2.9 by Matej Koval

Facebook Share

Share on facebook

Accordion FAQ

mod_joomtouch

Version Iphone

Version Iphone by JoomTouch