How To Convert Array To XML On PHP



i have PHP code like this:



public function get_xml_product($serviceName) {
$product = Array();
$idx = $idx2 = 0;
$explodeResult=explode(", ",$serviceName);
foreach($explodeResult as $value)
{
$sql = "SELECT id,parent,code,name,xs1 as DOWNLOAD, xs2 as UPLOAD
FROM sc_params
WHERE rfen = 'SERVICE_REQUESTED'
AND code = '".$value."'
OR code IN
(
SELECT code
FROM sc_params
WHERE rfen = 'SERVICE_REQUESTED'
AND id = (
SELECT parent
FROM sc_params
WHERE rfen = 'SERVICE_REQUESTED'
AND code = '".$value."' )
)
";
$stmt = oci_parse($this->_conn,$sql);
$rs = oci_execute($stmt);
if (!$rs) {
$error = oci_error();
$this->_err = $error;
return false;
}

while ($data = oci_fetch_array($stmt, OCI_BOTH)) {
if($data['PARENT'] == 0) {
$idx++;
$product[$idx]['id'] = $data['ID'];
$product[$idx]['name'] = $data['NAME'];
}
else {
foreach($product as $product2)
{
$product[$idx]['download'][$idx2]['name'] = 'DOWNLOAD';
$product[$idx]['download'][$idx2]['value'] = $data['DOWNLOAD'];
$product[$idx]['upload'][$idx2]['name'] = 'UPLOAD';
$product[$idx]['upload'][$idx2]['value'] = $data['UPLOAD'];
$idx2++;
}
}
}
}
print_r($product);
......


the result show:



Array
(
[1] => Array
(
[id] => 1
[name] => INTERNET
[download] => Array
(
[0] => Array
(
[name] => DOWNLOAD
[value] => 1024
)

)

[upload] => Array
(
[0] => Array
(
[name] => UPLOAD
[value] => 256
)

)

)


and i want to make it into xml like this one:



<xml version="1.0"?>
<services>
<service>
<id>1</id>
<name>INTERNET</name>
<attribute>
<name>DOWNLOAD</name>
<value>1024</value>
</attribute>
<attribute>
<name>UPLOAD</name>
<value>512</value>
</attribute>
</service>
</services>


Please Help me....


No comments:

Post a Comment