PHP中將數(shù)組轉(zhuǎn)成XML格式的實(shí)現(xiàn)代碼
更新時(shí)間:2011年08月08日 18:58:15 作者:
網(wǎng)上找的一段代碼! 然后我自己根據(jù)php DOMDocument又寫了一段代碼,需要的朋友可以參考下。
下面是網(wǎng)上的
class ArrayToXML
{
/**
* The main function for converting to an XML document.
* Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
*
* @param array $data
* @param string $rootNodeName - what you want the root node to be - defaultsto data.
* @param SimpleXMLElement $xml - should only be used recursively
* @return string XML
*/
public static function toXml($data, $rootNodeName = 'data', $xml=null)
{
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1)
{
ini_set ('zend.ze1_compatibility_mode', 0);
}
if ($xml == null)
{
$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
}
// loop through the data passed in.
foreach($data as $key => $value)
{
// no numeric keys in our xml please!
if (is_numeric($key))
{
// make string key...
$key = "unknownNode_". (string) $key;
}
// replace anything not alpha numeric
$key = preg_replace('/[^a-z]/i', '', $key);
// if there is another array found recrusively call this function
if (is_array($value))
{
$node = $xml->addChild($key);
// recrusive call.
ArrayToXML::toXml($value, $rootNodeName, $node);
}
else
{
// add single node.
$value = htmlentities($value);
$xml->addChild($key,$value);
}
}
// pass back as string. or simple xml object if you want!
return $xml->asXML();
}
}
下面是我編輯過的代碼
function arrtoxml($arr,$dom=0,$item=0){
if (!$dom){
$dom = new DOMDocument("1.0");
}
if(!$item){
$item = $dom->createElement("root");
$dom->appendChild($item);
}
foreach ($arr as $key=>$val){
$itemx = $dom->createElement(is_string($key)?$key:"item");
$item->appendChild($itemx);
if (!is_array($val)){
$text = $dom->createTextNode($val);
$itemx->appendChild($text);
}else {
arrtoxml($val,$dom,$itemx);
}
}
return $dom->saveXML();
}
數(shù)組轉(zhuǎn)換成XML格式
<?
$elementLevel = 0 ;
function array_Xml($array, $keys = '')
{
global $elementLevel;
if(!is_array($array))
{
if($keys == ''){
return $array;
}else{
return "\n<$keys>" . $array . "</$keys>";
}
}else{
foreach ($array as $key => $value)
{
$haveTag = true;
if (is_numeric($key))
{
$key = $keys;
$haveTag = false;
}
/**
* The first element
*/
if($elementLevel == 0 )
{
$startElement = "<$key>";
$endElement = "</$key>";
}
$text .= $startElement."\n";
/**
* Other elements
*/
if(!$haveTag)
{
$elementLevel++;
$text .= "<$key>" . array_Xml($value, $key). "</$key>\n";
}else{
$elementLevel++;
$text .= array_Xml($value, $key);
}
$text .= $endElement."\n";
}
}
return $text;
}
?>
函數(shù)描述及例子
<?
$array = array(
"employees" => array(
"employee" => array(
array(
"name" => "name one",
"position" => "position one"
),
array(
"name" => "name two",
"position" => "position two"
),
array(
"name" => "name three",
"position" => "position three"
)
)
)
);
echo array_Xml($array);
?>
復(fù)制代碼 代碼如下:
class ArrayToXML
{
/**
* The main function for converting to an XML document.
* Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
*
* @param array $data
* @param string $rootNodeName - what you want the root node to be - defaultsto data.
* @param SimpleXMLElement $xml - should only be used recursively
* @return string XML
*/
public static function toXml($data, $rootNodeName = 'data', $xml=null)
{
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1)
{
ini_set ('zend.ze1_compatibility_mode', 0);
}
if ($xml == null)
{
$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
}
// loop through the data passed in.
foreach($data as $key => $value)
{
// no numeric keys in our xml please!
if (is_numeric($key))
{
// make string key...
$key = "unknownNode_". (string) $key;
}
// replace anything not alpha numeric
$key = preg_replace('/[^a-z]/i', '', $key);
// if there is another array found recrusively call this function
if (is_array($value))
{
$node = $xml->addChild($key);
// recrusive call.
ArrayToXML::toXml($value, $rootNodeName, $node);
}
else
{
// add single node.
$value = htmlentities($value);
$xml->addChild($key,$value);
}
}
// pass back as string. or simple xml object if you want!
return $xml->asXML();
}
}
下面是我編輯過的代碼
復(fù)制代碼 代碼如下:
function arrtoxml($arr,$dom=0,$item=0){
if (!$dom){
$dom = new DOMDocument("1.0");
}
if(!$item){
$item = $dom->createElement("root");
$dom->appendChild($item);
}
foreach ($arr as $key=>$val){
$itemx = $dom->createElement(is_string($key)?$key:"item");
$item->appendChild($itemx);
if (!is_array($val)){
$text = $dom->createTextNode($val);
$itemx->appendChild($text);
}else {
arrtoxml($val,$dom,$itemx);
}
}
return $dom->saveXML();
}
數(shù)組轉(zhuǎn)換成XML格式
復(fù)制代碼 代碼如下:
<?
$elementLevel = 0 ;
function array_Xml($array, $keys = '')
{
global $elementLevel;
if(!is_array($array))
{
if($keys == ''){
return $array;
}else{
return "\n<$keys>" . $array . "</$keys>";
}
}else{
foreach ($array as $key => $value)
{
$haveTag = true;
if (is_numeric($key))
{
$key = $keys;
$haveTag = false;
}
/**
* The first element
*/
if($elementLevel == 0 )
{
$startElement = "<$key>";
$endElement = "</$key>";
}
$text .= $startElement."\n";
/**
* Other elements
*/
if(!$haveTag)
{
$elementLevel++;
$text .= "<$key>" . array_Xml($value, $key). "</$key>\n";
}else{
$elementLevel++;
$text .= array_Xml($value, $key);
}
$text .= $endElement."\n";
}
}
return $text;
}
?>
函數(shù)描述及例子
復(fù)制代碼 代碼如下:
<?
$array = array(
"employees" => array(
"employee" => array(
array(
"name" => "name one",
"position" => "position one"
),
array(
"name" => "name two",
"position" => "position two"
),
array(
"name" => "name three",
"position" => "position three"
)
)
)
);
echo array_Xml($array);
?>
您可能感興趣的文章:
- 遞歸實(shí)現(xiàn)php數(shù)組轉(zhuǎn)xml的代碼分享
- PHP處理數(shù)組和XML之間的互相轉(zhuǎn)換
- php實(shí)現(xiàn)將數(shù)組轉(zhuǎn)換為XML的方法
- php下將XML轉(zhuǎn)換為數(shù)組
- PHP如何將XML轉(zhuǎn)成數(shù)組
- PHP實(shí)現(xiàn)的數(shù)組和XML文件相互轉(zhuǎn)換功能示例
- PHP將XML轉(zhuǎn)數(shù)組過程詳解
- php中Array2xml類實(shí)現(xiàn)數(shù)組轉(zhuǎn)化成XML實(shí)例
- php實(shí)現(xiàn)xml轉(zhuǎn)換數(shù)組的方法示例
- PHP實(shí)現(xiàn)數(shù)組array轉(zhuǎn)換成xml的方法
- php實(shí)現(xiàn)的數(shù)組轉(zhuǎn)xml案例分析
相關(guān)文章
PHP5全版本繞過open_basedir讀文件腳本漏洞詳細(xì)介紹
這篇文章主要介紹了PHP5全版本繞過open_basedir讀文件腳本漏洞詳細(xì)介紹,這個(gè)漏洞很久之前(大概5年前)被提出來了,到現(xiàn)在的最新版本中依然存在,需要的朋友可以參考下2015-01-01php實(shí)現(xiàn)簡(jiǎn)單的守護(hù)進(jìn)程創(chuàng)建、開啟與關(guān)閉操作
這篇文章主要介紹了php實(shí)現(xiàn)簡(jiǎn)單的守護(hù)進(jìn)程創(chuàng)建、開啟與關(guān)閉操作,結(jié)合實(shí)例形式分析了基于pcntl擴(kuò)展的php守護(hù)進(jìn)程類定義、啟動(dòng)及關(guān)閉等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08PHP使用mysql_fetch_row查詢獲得數(shù)據(jù)行列表的方法
這篇文章主要介紹了PHP使用mysql_fetch_row查詢獲得數(shù)據(jù)行列表的方法,涉及php中使用mysql_fetch_row操作數(shù)據(jù)庫的技巧,需要的朋友可以參考下2015-03-03PHP中使用cURL實(shí)現(xiàn)Get和Post請(qǐng)求的方法
PHP中使用cURL實(shí)現(xiàn)Get和Post請(qǐng)求的方法,需要的朋友可以參考一下2013-03-03