• 0

[PHP] Xml To Array help


Question

I hope this makes since to someone, and I'm sorry for the huge blocks of text.

If i use the following function:

function xml_to_array($raw_xml, $page)
{
	$xml_parser = xml_parser_create();
	xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 0);
	xml_parse_into_struct($xml_parser, $raw_xml, $vals);
	xml_parser_free($xml_parser);

	$_tmp = '';
	foreach ($vals as $xml_elem)
	{
		$x_tag = $xml_elem['tag'];
		$x_level = $xml_elem['level'];
		$x_type = $xml_elem['type'];

		if ($x_level != 1 && $x_type == 'close')
		{
			if (isset($multi_key[$x_tag][$x_level]))
				$multi_key[$x_tag][$x_level] = 1;
			else
				$multi_key[$x_tag][$x_level] = 0;
		}

		if ($x_level != 1 && $x_type == 'complete')
		{
			if ($_tmp == $x_tag)
				$multi_key[$x_tag][$x_level] = 1;

			$_tmp = $x_tag;
		}
	}

	foreach ($vals as $xml_elem)
	{
		$x_tag = $xml_elem['tag'];
		$x_level = $xml_elem['level'];
		$x_type = $xml_elem['type'];

		if ($x_type == 'open')
			$level[$x_level] = $x_tag;

		$start_level = 1;
		$php_stmt = '$xml_array';
		if ($x_type == 'close' && $x_level != 1)
			$multi_key[$x_tag][$x_level]++;

		while ($start_level < $x_level)
		{
			$php_stmt .= '[$level['.$start_level.']]';
			if (isset($multi_key[$level[$start_level]][$start_level]) && $multi_key[$level[$start_level]][$start_level])
				$php_stmt .= '['.($multi_key[$level[$start_level]][$start_level]).'-1]';

			++$start_level;
		}

		$add = '';
		if (isset($multi_key[$x_tag][$x_level]) && $multi_key[$x_tag][$x_level] && ($x_type == 'open' || $x_type == 'complete'))
		{
			if (!isset($multi_key2[$x_tag][$x_level]))
				$multi_key2[$x_tag][$x_level] = 0;
			else
				$multi_key2[$x_tag][$x_level]++;

			$add = '['.$multi_key2[$x_tag][$x_level].']';
		}

		if (array_key_exists('attributes', $xml_elem))
		{
			foreach ($xml_elem['attributes'] as $key=>$value)
			{
				$php_stmt_att=$php_stmt.'[$x_tag]'.$add.'[$key] = $value;';
				eval($php_stmt_att);
			}
		}
	}

	return $xml_array;
}

..on the following block of xml:

<achievements lang="en_us" requestUrl="/character-achievements.xml">
	<category>
		<achievement categoryId="92" dateCompleted="2009-02-05-05:00" desc="Equip Haris Pilton's "Gigantique" Bag." icon="inv_misc_bag_27" id="1165" points="10" title="My Sack is "Gigantique""/>
		<achievement categoryId="92" dateCompleted="2009-01-26-05:00" desc="Loot 1,000 gold." icon="inv_misc_coin_04" id="1177" points="20" title="Got My Mind On My Money">
			<achievement categoryId="92" dateCompleted="2008-12-25-05:00" desc="Loot 100 gold." icon="inv_misc_coin_06" id="1176" points="10" title="Got My Mind On My Money"/>
		</achievement>
		<achievement categoryId="92" dateCompleted="2009-01-18-05:00" desc="Obtain a Mekgineer's Chopper or a Mechano-hog." icon="inv_misc_key_14" id="2097" points="10" title="Get to the Choppa!"/>
		<achievement categoryId="92" dateCompleted="2009-01-11-05:00" desc="Raise your unarmed skill to 400." icon="ability_warrior_secondwind" id="16" points="10" title="Did Somebody Order a Knuckle Sandwich?">
			<criteria maxQuantity="400" quantity="400"/>
		</achievement>
	</category>
</achievements>

i come up with the following array:

Array
(
    [achievements] => Array
        (
            [lang] => en_us
            [requestUrl] => /character-achievements.xml
            [category] => Array
                (
                    [achievement] => Array
                        (
                            [0] => Array
                                (
                                    [categoryId] => 92
                                    [dateCompleted] => 2009-02-05-05:00
                                    [desc] => Equip Haris Pilton's "Gigantique" Bag.
                                    [icon] => inv_misc_bag_27
                                    [id] => 1165
                                    [points] => 10
                                    [title] => My Sack is "Gigantique"
                                    [achievement] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [categoryId] => 92
                                                    [dateCompleted] => 2008-12-25-05:00
                                                    [desc] => Loot 100 gold.
                                                    [icon] => inv_misc_coin_06
                                                    [id] => 1176
                                                    [points] => 10
                                                    [title] => Got My Mind On My Money
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [categoryId] => 92
                                    [dateCompleted] => 2009-01-26-05:00
                                    [desc] => Loot 1,000 gold.
                                    [icon] => inv_misc_coin_04
                                    [id] => 1177
                                    [points] => 20
                                    [title] => Got My Mind On My Money
                                    [criteria] => Array
                                        (
                                            [maxQuantity] => 400
                                            [quantity] => 400
                                        )

                                )

                            [2] => Array
                                (
                                    [categoryId] => 92
                                    [dateCompleted] => 2009-01-18-05:00
                                    [desc] => Obtain a Mekgineer's Chopper or a Mechano-hog.
                                    [icon] => inv_misc_key_14
                                    [id] => 2097
                                    [points] => 10
                                    [title] => Get to the Choppa!
                                )

                            [3] => Array
                                (
                                    [categoryId] => 92
                                    [dateCompleted] => 2009-01-11-05:00
                                    [desc] => Raise your unarmed skill to 400.
                                    [icon] => ability_warrior_secondwind
                                    [id] => 16
                                    [points] => 10
                                    [title] => Did Somebody Order a Knuckle Sandwich?
                                )

                        )

                )

        )

)

my problem is that in the array some parts are not in the correct place.

"/achievements/category/achievement/0/achievement/"

should be down on

"/achievements/category/achievement/1/"

also..

"/achievements/category/achievement/1/criteria/"

should be down on

"/achievements/category/achievement/3/

any help to decode this function would be greatly appreciated.

iv attached a test.php file to help out.

test.php

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Can no one help me figure why in the outputted array, some parts are not in the correct location? Or maybe suggest a different way to convert XML into an array?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.