Reply to Re: [PHP] bad use of foreach() when building xml str from xml tree(source code provided here).

Your name:

Reply:


Posted by "Scott Fletcher" on 09/29/05 00:43

Here's we go. A better script here. Now I only need to figure out how to
add data to the string and assigned it to string 1 when done. I used hte
break command there to make a point here. Maybe this will work...

--snip--
$array = array
(
'NEWSFEED' => array
(
'0' => array
(
'MESSAGE' => array
(
'0' => array
(
'COMMENT' => array
(
'0' => array
(
'VALUE' => 'Comment #1'
)
)
),
'1' => array
(
'COMMENT' => array
(
'0' => array
(
'VALUE' => 'Comment #2'
)
)
)
)
)
)
);

function GetXmlStr($tree)
{
$branch_level = 0; //Tree's branches level...

XmlTreeDecompile($tree, $branch_level, $str1, $str2, $str3);
//echo $str1." #<br>";
//echo $str2." #<br>";
//echo $str3." #<br>";

return htmlentities($str1.$str2.$str3);
}
function XmlTreeDecompile($tree, $branch_level, &$str1, &$str2, &$str3)
{
$branch_level++;

foreach($tree as $key => $value)
{
if ($branch_level % 2 != 0) { //xml name-tag counter...
if(!(is_array($value))) {
$str2 .= str_repeat("
",$branch_level)."<".$key.">".$value."</".$key.">"."\r\n";
} else {
$str1 .= str_repeat(" ",$branch_level)."<".$key.">"."\r\n";
XmlTreeDecompile($value, $branch_level, $str1, $str2, $str3);
$str3 .= str_repeat(" ",$branch_level)."</".$key.">"."\r\n";
}
} else { //Xml tag's name...
//echo $key." #<br>";
if ($key != 0) {
break;
//$str1 .= $str2;
//$str2 = "";
}
XmlTreeDecompile($value, $branch_level, $str1, $str2, $str3);
}
}
echo $str1." \n--\n";
echo $str2." \n--\n";
echo $str3." \n--\n";
}

$xml_string = GetXmlStr($array);

echo "\n<br><br>\n".$xml_string;
--snip--

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация