我給你2個功能,一個用於xml2array轉換,另一個用於array2xml轉換
function xml2ary(&$string)
{
$parser = xml_parser_create();
xml_parser_set_option ($parser , XML_OPTION_CASE_FOLDING , 0);
xml_parse_into_struct ($parser , $string , $vals , $index);
xml_parser_free ($parser);
$mnary = array();
$ary = &$mnary;
foreach ($vals as $r)
{
$t = $r['tag'];
if ($r['type'] == 'open')
{
if (isset ($ary[$t]))
{
if (isset ($ary[$t][0]))
$ary[$t][] = array();
else
$ary[$t] = array($ary[$t] , array());
$cv = &$ary[$t][count ($ary[$t]) - 1];
}
else
$cv = &$ary[$t];
if (isset ($r['attributes']))
{
foreach ($r['attributes'] as $k => $v)
$cv['_a'][$k] = $v;
}
$cv['_c'] = array();
$cv['_c']['_p'] = &$ary;
$ary = &$cv['_c'];
}
elseif ($r['type'] == 'complete')
{
if (isset ($ary[$t]))
{ // same as open
if (isset ($ary[$t][0]))
$ary[$t][] = array();
else
$ary[$t] = array($ary[$t] , array());
$cv = &$ary[$t][count ($ary[$t]) - 1];
}
else
$cv = &$ary[$t];
if (isset ($r['attributes']))
{
foreach ($r['attributes'] as $k => $v)
$cv['_a'][$k] = $v;
}
$cv['_v'] = (isset ($r['value']) ? $r['value'] : '');
}
elseif ($r['type'] == 'close')
{
$ary = &$ary['_p'];
}
}
_del_p ($mnary);
return $mnary;
}
function ary2xml($cary , $d = 0 , $forcetag = '')
{
$res = array();
foreach ($cary as $tag => $r)
{
if (isset ($r[0]))
{
$res[] = ary2xml ($r , $d , $tag);
}
else
{
if ($forcetag)
$tag = $forcetag;
$sp = str_repeat ("\t" , $d);
$res[] = "$sp<$tag";
if (isset ($r['_a']))
{
foreach ($r['_a'] as $at => $av)
$res[] = " $at=\"$av\"";
}
$res[] = ">" . ((isset ($r['_c'])) ? "\n" : '');
if (isset ($r['_c']))
$res[] = ary2xml ($r['_c'] , $d + 1);
elseif (isset ($r['_v']))
$res[] = $r['_v'];
$res[] = (isset ($r['_c']) ? $sp : '') . "</$tag>\n";
}
}
return implode ('' , $res);
}
通過你的XML功能xml2ary(),你會得到輸出如下圖所示
Array
(
[xml] => Array
(
[_c] => Array
(
[settings] => Array
(
[_c] => Array
(
[title] => Array
(
[_v] => Calendar2
)
[subTitle] => Array
(
[_v] => Calendar2
)
)
)
[events] => Array
(
[_a] => Array
(
[date] => 02-09-2010
)
[_c] => Array
(
[event] => Array
(
[_c] => Array
(
[title] => Array
(
[_v] => HTML Tags
)
[description] => Array
(
[_v] => HTML Tags
)
)
)
)
)
)
)
)
然後轉化這個數組你想如何。
例如。
Array
(
[xml] => Array
(
[_c] => Array
(
[settings] => Array
(
[_c] => Array
(
[title] => Array
(
[_v] => Calendar2
)
[subTitle] => Array
(
[_v] => Calendar2
)
)
)
[events] => Array
(
[_a] => Array
(
[date] => 02-09-2010
)
[_c] => Array
(
[event] => Array
(
[0] => Array
(
[_c] => Array
(
[title] => Array
(
[_v] => HTML Tags
)
[description] => Array
(
[_v] => HTML Tags
)
)
)
[1] => Array
(
[_c] => Array
(
[title] => Array
(
[_v] => Another Title
)
[description] => Array
(
[_v] => Another description
)
)
)
)
)
)
)
)
)
,並再次使用ary2xml()函數
你會得到你想要的輸出..
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<settings>
<title>Calendar2</title>
<subTitle>Calendar2</subTitle>
</settings>
<events date="02-09-2010">
<event>
<title>HTML Tags</title>
<description>HTML Tags</description>
</event>
<event>
<title>Another Title</title>
<description>Another description</description>
</event>
</events>
</xml>
能解釋更多關於它用一個例子 – Warrior 2010-09-02 05:22:59
我這樣做一個 $ xml = new SimpleXMLElement($ xml_str); $ event = $ xml-> events-> addChild('event'); $ event-> addChild('title','More Parser Stories'); $ event-> addChild('description','這是所有關於使它工作的人。'); file_put_contents($ xmlfile,$ xml-> asXML()); 我怎麼能給條件 – Warrior 2010-09-02 06:02:35
@THOmas,你解決了你的問題? – systemovich 2010-09-02 07:32:46