0

I have XML file content as following:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet">
    <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"></OfficeDocumentSettings>
    <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"></ExcelWorkbook>
    <Worksheet ss:Name="Sheet 1">
        <Table>
            <Row>
                <Cell>
                    <Data ss:Type="String">store</Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">websites</Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">attribute_set</Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">type</Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">category_ids</Data>
                </Cell>
            </Row>
            <Row>
                <Cell>
                    <Data ss:Type="String">admin</Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">base</Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">Books</Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">simple</Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">2,4,276,280</Data>
                </Cell>
            </row>
        </Table>
    </Worksheet>
</Workbook>

I have to convert each row as array element. For this I am using:

$xml = simplexml_load_file('list_product.xml');

I am able to get array of row but for cell I am getting only SimpleXMLElement object.

dur
  • 15,689
  • 25
  • 79
  • 125
Magecode
  • 225
  • 2
  • 6
  • 16

2 Answers2

0

Use simple function like this:

$output = null;
function simpleXmlElementToArray(&$output, \SimpleXmlElement $element)
{
    $output = (array)$element;
    foreach ($output as $k => $v) {
        if ($v instanceof \SimpleXMLElement) {
            simpleXmlElementToArray($output[$k], $v);
        }
    }
}
Daniel Hornik
  • 1,957
  • 1
  • 14
  • 33
0
function xmlToArray($xml){
    $xml = str_replace(array("\n", "\r", "\t"), '', $xml);
    // $xml = trim(str_replace('"', "'", $xml));
    $xml = trim($xml);
    $start_tree = (array) simplexml_load_string($xml);

    $final_tree = array();
    loopRecursivelyForAttributes($start_tree, $final_tree);
    return $final_tree;
}

function loopRecursivelyForAttributes($start_tree, &$final_tree){
    foreach($start_tree as $key1 => $row1){
        if(!array_key_exists($key1, $final_tree)){
            $final_tree[$key1] = array();
        }

        // If there is only one sub node, then there will be one less
        // array - ie: $row1 will be an array which has an '@attributes' key
        if(array_key_exists('@attributes', $row1)){
            $row1 = (array) $row1;
            getValues($start_tree, $final_tree, $key1, $row1);
        }
        else{
            foreach ($row1 as $row2){
                $row2 = (array) $row2;
                getValues($start_tree, $final_tree, $key1, $row2);
            }
        }
    }
}

function getValues($start_tree, &$final_tree, $key1, $row2){
    foreach ($row2 as $key3 => $val3){
        $val3 = (array) $val3;

        if($key3 == '@attributes'){
            $final_tree[$key1][] = $val3;
        }
        else{
            $temp_parent = array();
            $temp_parent[$key3] = $val3;
            loopRecursivelyForAttributes($temp_parent, $final_tree[$key1][count($final_tree[$key1]) - 1]);
        }
    }
}

//usage
$xml = xmlToArray('xml as string');
Vijay Dohare
  • 731
  • 5
  • 22