xml

26 views
Skip to first unread message

fz2.shahmohammadi

unread,
Sep 15, 2012, 4:02:29 AM9/15/12
to web_desig...@googlegroups.com

سلام دوستان.

کسی میدونه چطوری میشه اطلاعات رو از بانک اطلاعاتی خوند و در یک فایل xml  ذخیره کنیم.؟؟؟؟؟

Message has been deleted

AiPdimi

unread,
Sep 16, 2012, 5:36:32 PM9/16/12
to web_desig...@googlegroups.com

سلام
خودت میتونی یه تابع درست کنی مثل 
json_encode()
به صورت زیر

function xml_encode($mixed,$domElement=null,$DOMDocument=null){
if(is_null($DOMDocument)){
$DOMDocument=new DOMDocument;
$DOMDocument->formatOutput=true;
xml_encode
($mixed,$DOMDocument,$DOMDocument);
echo $DOMDocument->saveXML();
}
else{
if(is_array($mixed)){
foreach($mixed as $index=>$mixedElement){
if(is_int($index)){
if($index==0){
$node=$domElement;
}
else{
$node=$DOMDocument->createElement($domElement->tagName);
$domElement->parentNode->appendChild($node);
}
}
else{
$plural=$DOMDocument->createElement($index);
$domElement->appendChild($plural);
$node=$plural;
if(rtrim($index,'s')!==$index){
$singular=$DOMDocument->createElement(rtrim($index,'s'));
$plural->appendChild($singular);
$node=$singular;
}
}
xml_encode
($mixedElement,$node,$DOMDocument);
}
}
else{
$domElement->appendChild($DOMDocument->createTextNode($mixed));
}
}
}

و تو
php
  اینطور استفاده کنی 

$data=array();
for($i=0;$i<3;$i++){
$data['users'][]=array(
'name'=>'user'.$i,
'img'=>'http://example.com/user'.$i.'.png',
'website'=>'http://example.com/'
);
}
 
header('Content-type: application/xml');
echo xml_encode($data);

که در نهایت خروجیش اینطور میشه

<users>
<user>
<name>user0</name>
<image>http://example.com/user0.png</image>
<website>http://example.com/</website>
</user>
<user>
<name>user1</name>
<image>http://example.com/user1.png</image>
<website>http://example.com/</website>
</user>
<user>
<name>user2</name>
<image>http://example.com/user2.png</image>
<website>http://example.com/</website>
</user>
</users>



و با سایت زیر رو ببین
http://darklaunch.com/2009/05/23/php-xml-encode-using-domdocument-convert-array-to-xml-json-encode

fz2.shahmohammadi

unread,
Sep 18, 2012, 4:42:07 AM9/18/12
to web_desig...@googlegroups.com
 خیلی ممنون از توضیحاتتون
موفق باشید
Reply all
Reply to author
Forward
0 new messages