You are here:  » XML to array


XML to array

Submitted by cowkie on Wed, 2007-02-07 14:07 in

Hi guys!

I have this problem:

<?php
    $struct 
'
<layoutitem>
    <name>scheme_4</name>
    <box>
        <cikkid>aid1</cikkid>
        <cikktitle>atitle1</cikktitle>
        <posx>1</posx>
        <posy>1</posy>
        <colspan>2</colspan>
        <rowspan>5</rowspan>
        <cikklead>alead1</cikklead>
    </box>
    <box>
        <cikkid>aid2</cikkid>
        <cikktitle>atitle2</cikktitle>
        <posx>1</posx>
        <posy>3</posy>
        <colspan>3</colspan>
        <rowspan>2</rowspan>
        <cikklead>alead2</cikklead>
    </box>
    <box>
        <cikkid>aid3</cikkid>
        <cikktitle>atitle3</cikktitle>
        <posx>3</posx>
        <posy>3</posy>
        <colspan>3</colspan>
        <rowspan>3</rowspan>
        <cikklead>alead3</cikklead>
    </box>
    <layoutid>4</layoutid>
</layoutitem>
    '
;
    require(
"MagicParser.php");
    function 
myRecordHandler($record)
    {
      global 
$arr;
      
$arr[] = $record;
    }
    
MagicParser_parse("string://".$struct,"myRecordHandler","xml|LAYOUTITEM/BOX");
?>

After that $arr is empty. Any idea?

Best regards,
-Ervin

Submitted by support on Wed, 2007-02-07 14:40

Hello Ervin,

All it is, there is a "/" missing off the end of the format string. You should be using:

MagicParser_parse("string://".$struct,"myRecordHandler","xml|LAYOUTITEM/BOX/");

I've just tried your code, and used print_r($arr) to display the array after the parse.

Hope this helps!
Regards,
David.

Submitted by cowkie on Wed, 2007-02-07 14:47

Thanks for the fast response David.

The problem is still appears after i inserted the '/'. $arr is still blank. I tried $arr = array(); too, but nothing.

Maybe it's a character encoding problem?

Ah, almost forgot. PHP drops this:

Notice: Undefined variable: arr in /srv/www/projects/test/webroot/functions.php on line 399

line 399 =

    print_r($arr);

Submitted by support on Wed, 2007-02-07 14:51

Hello Ervin,

The PHP warning notice can be removed by initialising the array as follows:

$arr = array();

I'll send you my script by email as an attachment for you to try...

Cheers,
David.