0

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>

UPDATED: my php file has condition for xml format... i tried:

$xml = new SimpleXMLElement("<services/>");
        foreach($product as $key => $value) {
            if (is_array($value)) {
                $subnode = $xml->addChild("service");
                $this->array_to_xml($value, $subnode);
            } 
            else
            {
               $xml->addChild(htmlspecialchars("$key"),htmlspecialchars("$value"));
            }
        } 
        $string = $xml->asXML(); 
        $result = htmlentities($string);

and it call array_to_xml, here it is:

public function array_to_xml($product, &$xml) {
        foreach($product as $key => $value) {
            if (is_array($value)) {
                $subnode = $xml->addChild("service");
                array_to_xml($value, $subnode);
            } else {
               $xml->addChild(htmlspecialchars("$key"),htmlspecialchars("$value"));
            }
        }
        return $xml;
    }

it gave error 500 Internal Server Error, but if i remove condition in php it run.

Please Help me....

DimasW
  • 181
  • 1
  • 3
  • 11
  • just use [`SimpleXML`](http://php.net/manual/en/book.simplexml.php), use the functions in creating nodes/attributes accordingly – Kevin Jan 29 '15 at 09:24

5 Answers5

1

There are heaps of PHP libraries for this, google might be a good place to start!: Php array to xml

theatlasroom
  • 1,126
  • 9
  • 12
1

You can use SimpleXML: here explained: How to convert array to SimpleXML

docs: http://php.net/manual/en/book.simplexml.php

Community
  • 1
  • 1
Phate01
  • 2,499
  • 2
  • 30
  • 55
1

You can use SimpleXML:get more help from here with proper discussion in comments. for your customized way try this.it should solve your problem:

<?php

$test_array=Array
    (
        '1' => Array
            (
                'id' => 1,
                'name' => 'INTERNET',
                'download' => Array
                    (
                        '0' => Array
                            (
                                'name' => 'DOWNLOAD',
                                'value' => 1024
                            )

                    ),

                'upload' => Array
                    (
                        '0' => Array
                            (
                                'name' => 'UPLOAD',
                                'value' => 256
                            )

                    )

            )
            );
// creating object of SimpleXMLElement
$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><services></services>");
$subnode=$xml->addChild("service");
array_to_xml($test_array[1],$subnode);

//saving generated xml file
print $xml->asXML('info.xml');// create an info.xml file is written successfully will print 1.

function array_to_xml($test_array, &$xml) {
    foreach($test_array as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml->addChild("$key");
                array_to_xml($value, $subnode);
            }
            else{
                $subnode = $xml->addChild("attribute");
                array_to_xml($value, $subnode);
            }
        }
        else {
            $xml->addChild("$key",htmlspecialchars("$value"));
        }
    }
}

?> 
Community
  • 1
  • 1
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
  • how can i add my php code to array again? @SuchitKumar . i use `print_r` before to show array result.. i tried `$test_array = print_r $product` but it don't show xml – DimasW Jan 29 '15 at 10:24
  • to see data of xml use `print $xml->asXML();` but it will not show the structure it will show values only.to see entire xml object use `print_r($xml);` – Suchit kumar Jan 29 '15 at 10:33
  • ok it works! thank u, but can i delete `` and `` in xml, cause i just need `` ..? @SuchitKumar – DimasW Jan 29 '15 at 11:19
  • i'll suggest do not construct this key at all if your are not using otherwise skip it at the time of xml creation. and if it really helped you then you can mark it as a answer. – Suchit kumar Jan 29 '15 at 11:50
0

A basic way of conversion:

    <?php

$test_array = array ( 'tmp1' => 'tmp2', 'tmp3' => 'tmp4', 'more_array' => array ( 'tmps' => 'tmpx', ),); $xml = new SimpleXMLElement('');array_walk_recursive($test_array, array ($xml, 'addChild'));print $xml->asXML();

result:

tmp1tmp3tmpx>tmps/root>

Keys & values are swapped - fixable with array_flip() before array_walk.

A_W_recursive needs PHP5, you can use a_w but 'tmps' => 'tmpx' wont work.

Another way (repeatable, for-based) can be seen here: https://www.kerstner.at/en/2011/12/php-array-to-xml-conversion/

Sorry for the coding, the code function on this site fails to code-tag XML. See image: https://i.stack.imgur.com/MJvI6.jpg

Overmind
  • 159
  • 10
0

Here is PHP 5.2 code which will convert array of any depth to xml document:

    Array
    (
    ['total_stud']=> 500
    [0] => Array
        (
            [student] => Array
                (
                    [id] => 1
                    [name] => abc
                    [address] => Array
                        (
                            [city]=>Lahore
                            [zip]=>54000
                        )                       
                )
        )
    [1] => Array
        (
            [student] => Array
                (
                    [id] => 2
                    [name] => xyz
                    [address] => Array
                        (
                            [city]=>Karachi
                            [zip]=>56000
                        )   
            )

        )
)

Generated XML would be as:

<?xml version="1.0"?>
<student_info>
    <total_stud>500</total_stud>
    <student>
        <id>1</id>
        <name>abc</name>
        <address>
            <city>Lahore</city>
            <zip>54000</zip>
        </address>
    </student>
    <student>
        <id>1</id>
        <name>abc</name>
        <address>
            <city>Karachi</city>
            <zip>56000</zip>
        </address>
    </student>
</student_info>

Here is the code Snippet

// creating object of SimpleXMLElement
$xml_student_info = new SimpleXMLElement("<?xml version=\"1.0\"?><student_info></student_info>");

// function call to convert array to xml
array_to_xml($student_info,$xml_student_info);

//saving generated xml file
$xml_student_info->asXML('file path and name');


// function defination to convert array to xml
function array_to_xml($student_info, &$xml_student_info) {
    foreach($student_info as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml_student_info->addChild("$key");
                array_to_xml($value, $subnode);
            }
            else{
                $subnode = $xml_student_info->addChild("item$key");
                array_to_xml($value, $subnode);
            }
        }
        else {
            $xml_student_info->addChild("$key",htmlspecialchars("$value"));
        }
    }
}

?>