您的位置:首页 > 编程语言 > PHP开发

php中处理xml的文档方法

2012-12-07 11:00 441 查看
需要在PHP.INI中打开对SimpleXML的支持。
变量$xml为XML的文件地址,可以使网上的地址。
调用xmltoarray函数,返回一个符合XML文档结果的数组。

function xmltoarray($xml) {
$array = ( array ) (simplexml_load_file ( $xml ));
foreach ( $array as $key => $item ) {
$array [$key] = $this -> structtoarray ( ( array ) $item );
}
return $this -> formatarray ( $array );
}

function formatarray($data) {
if (is_array ( $data )) {
foreach ( $data as $key => $value ) {
if (count ( $data ) == '1') {
if (! is_array ( $value )) {
return $value;
}
}
$array [$key] = $this -> formatarray ( $value );
}
} else {
return $data;
}
return $array;
}

function structtoarray($item) {
if (! is_string ( $item )) {
$item = ( array ) $item;
foreach ( $item as $key => $val ) {
$item [$key] = $this -> structtoarray ( $val );
}
}
return $item;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: