xml转化成php数组(文件夹读取,XML解析)
发布日期:2021-11-07 23:20:57 浏览次数:7 分类:技术文章

本文共 3434 字,大约阅读时间需要 11 分钟。

由于目前php使用simple_load_file和xpath解析xml数据相当慢,然后实现了以下程序直接把XML文件转化成PHP文件,即直接在PHP文件中定义数组包含XML数据,然后在使用的地方直接加载此PHP文件,引用里面的数组。现在给出代码,里面包含对文件夹中所有文件的读取,递归建立目录,XML读取等相关点。

1.xml解析成数组类

getName(); $attributes = $node->attributes(); $children = $node->children (); //遍历节点上的所有属性 foreach( $attributes as $attrname => $attrvalue ) { $attrvalue = ( string )$attrvalue ; $node_attrbutes[ $attrname ] = trim( $attrvalue ); } //建立节点数组,包含contentAsName和attributesAsName两个数组键 //如果$node中的content没有设置,则为空 (eg:
这个地方没有任何值
) $content = ""; if( count($children) == 0 ) { $content = ( string ) $node; //此处会默认给出$node所代表结点的content值 } $node_array = array( self::$attributesAsName => $node_attrbutes , self::$contentAsName => trim( $content ) ); //设置层级关系 if( !isset( $parent_node[ $node_name ] ) ) { $is = count( $parent_node ); if( !isset( $parent_node[ self::$attributesAsName ] ) && count( $parent_node ) > 0 ) { $last_index = count( $parent_node ) -1; $parent_node =& $parent_node[ $last_index ]; $parent_node[ $node_name ] = $node_array; } else { $parent_node[ $node_name ] = $node_array; } } else { $append = &$parent_node[ $node_name ]; if( isset( $append[ self::$attributesAsName ] ) ) { $parent_node[ $node_name ] = array( $append ); $append = &$parent_node[ $node_name ]; } if( isset( $append[ $node_index ] ) ) { $append = &$append[ $node_index ]; } // 追加 array_push( $append , $node_array ); } $index = 0 ; // 递归操作 foreach( $children as $childnode ) { $parent = &$parent_node[ $node_name ]; self::createXMLArray( $childnode, $parent, $index++ ); } return $parent_node ; } public static function parseXml( $isfromstring=false, $isjson=false ) { if( $isfromstring ) { $root = @simplexml_load_string ( self::$xml ) ; } else { $root = @simplexml_load_file ( self::$xml) ; } if ( $root !== false) { $parent_node = array(); $array = self::createXMLArray( $root ,$parent_node ); return $isjson ? json_encode( $array ) : $array ; } return false; } public static function getXMLByName( $name, $ext = null ) { if( !isset(self::$xmlPool[$name]) ) { if( $ext == null ) { $ext = 'xml'; self::setXml("$name.$ext"); } else { self::setXml($name); } $ret = self::parseXml(); if ( $ret !== false ) { self::$xmlPool[$name] = $ret; } } if( isset(self::$xmlPool[$name]) ) return self::$xmlPool[$name]; else return false; }}?>
2.读取文件夹下所有配置文件并进行转化操作
$child ) { if( is_array($child) ) { //递归操作 xml_to_php($xmlpath, $phppath, &$child); } else { $array1 = Array($xmlpath, ".xml"); $array2 = Array($phppath, ".php"); $newpath = str_replace($array1, $array2, $child); Directory(dirname($newpath)); $result = var_export (XmlToArray::getXMLByName($child, true), true); $fp = fopen($newpath, "w"); $name = basename($newpath, ".php"); fwrite($fp, "
"); fclose($fp); } }}function do_change( $xmlpath, $phppath ){ //首先建立主目录 Directory($phppath); $files = file_list($xmlpath, '/\.xml$/i'); xml_to_php($xmlpath, $phppath, &$files);}//此处可以更改到相应的文件目录do_change((dirname(__FILE__) . "\config"), (dirname(__FILE__) . "\config2"));echo "

change success

";?>

转载地址:https://blog.csdn.net/zhangwu416826/article/details/7774148 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:boost bind 实现方法
下一篇:iostream iterator的使用

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月07日 23时34分16秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章