PHP,解析,xml格式数据,工具类,解析xml
后端开发-php教程
class ome_xml { /** * xml资源 * * @var resource * @see xml_parser_create() */ public $parser; /** * 资源编码 * * @var string */ public $srcenc; /** * target encoding * * @var string */ public $dstenc; /** * the original struct * * @access private * @var array */ public $_struct = array(); /** * Constructor * * @access public * @param mixed [$srcenc] source encoding * @param mixed [$dstenc] target encoding * @return void * @since */ function SofeeXmlParser($srcenc = null, $dstenc = null) { $this->srcenc = $srcenc; $this->dstenc = $dstenc; // initialize the variable. $this->parser = null; $this->_struct = array(); } /** * Parses the XML file * * @access public * @param string [$file] the XML file name * @return void * @since */ function xml2array($file) { //$this->SofeeXmlParser('utf-8'); $data = file_get_contents($file); $this->parseString($data); return $this->getTree(); } function xml3array($file){ $data = file_get_contents($file); $this->parseString($data); return $this->_struct; } /** * Parses a string. * * @access public * @param string data XML data * @return void */ function parseString($data) { if ($this->srcenc === null) { $this->parser = xml_parser_create(); } else { if($this->parser = xml_parser_create($this->srcenc)) { return 'Unable to create XML parser resource with '. $this->srcenc .' encoding.'; } } if ($this->dstenc !== null) { @xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->dstenc) or die('Invalid target encoding'); } xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); // lowercase tags xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); // skip empty tags if (!xml_parse_into_struct($this->parser, $data, $this->_struct)) { /*printf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser) );*/ $this->free(); return false; } $this->_count = count($this->_struct); $this->free(); } /** * return the data struction * * @access public * @return array */ function getTree() { $i = 0; $tree = array(); $tree = $this->addNode( $tree, $this->_struct[$i]['tag'], (isset($this->_struct[$i]['value'])) ? $this->_struct[$i]['value'] : '', (isset($this->_struct[$i]['attributes'])) ? $this->_struct[$i]['attributes'] : '', $this->getChild($i) ); unset($this->_struct); return $tree; } /** * recursion the children node data * * @access public * @param integer [$i] the last struct index * @return array */ function getChild(&$i) { // contain node data $children = array(); // loop while (++$i _count) { // node tag name $tagname = $this->_struct[$i]['tag']; $value = isset($this->_struct[$i]['value']) ? $this->_struct[$i]['value'] : ''; $attributes = isset($this->_struct[$i]['attributes']) ? $this->_struct[$i]['attributes'] : ''; switch ($this->_struct[$i]['type']) { case 'open': // node has more children $child = $this->getChild($i); // append the children data to the current node $children = $this->addNode($children, $tagname, $value, $attributes, $child); break; case 'complete': // at end of current branch $children = $this->addNode($children, $tagname, $value, $attributes); break; case 'cdata': // node has CDATA after one of it's children $children['value'] .= $value; break; case 'close': // end of node, return collected data return $children; break; } } //return $children; } /** * Appends some values to an array * * @access public * @param array [$target] * @param string [$key] * @param string [$value] * @param array [$attributes] * @param array [$inner] the children * @return void * @since */ function addNode($target, $key, $value = '', $attributes = '', $child = '') { if (!isset($target[$key]['value']) && !isset($target[$key][0])) { if ($child != '') { $target[$key] = $child; } if ($attributes != '') { foreach ($attributes as $k => $v) { $target[$key][$k] = $v; } } $target[$key]['value'] = $value; } else { if (!isset($target[$key][0])) { // is string or other $oldvalue = $target[$key]; $target[$key] = array(); $target[$key][0] = $oldvalue; $index = 1; } else { // is array $index = count($target[$key]); } if ($child != '') { $target[$key][$index] = $child; } if ($attributes != '') { foreach ($attributes as $k => $v) { $target[$key][$index][$k] = $v; } } $target[$key][$index]['value'] = $value; } return $target; } /** * Free the resources * * @access public * @return void **/ function free() { if (isset($this->parser) && is_resource($this->parser)) { xml_parser_free($this->parser); unset($this->parser); } }}
PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
能不能拿到maka的源码,ubuntu ssh欢迎,如何验证tomcat7,爬虫兼职项目,php语言集成开发环境,短视频关键词推广seo优化lzw
在线XML/JSON互相转换工具:http://tools.jb51.net/code/xmljson
discuz分类信息源码,vscode配置svn,ubuntu编程,tomcat 升级,sqlite 封装,网页设计图库,mysql5.5数据库租用,2003服务器搭建php环境,javascript 报表插件,前端项目框架,爬虫赚钱,php验证码,seo软文,springboot什么时候出来的,wordpress 调用文章标签,dede电影网站模版,网页加载h5游戏的代码,html5 意见反馈表模板,带dede后台的整套网站源码 数据库连接不上,页面加载进度条原理,快递员管理系统模板,小偷镜像程序lzw
在线格式化XML/在线压缩XML:http://tools.jb51.net/code/xmlformat
微网站源码开源,vscode颜色组合,ubuntu设置共享文件夹,tomcat 监听,sqlite 条件语句,网页设计怎样才算侵权,数据库create语句,新网云邮企业邮箱 找不到服务器,jquery 文件批量上传插件,安硕前端框架,爬虫素材,序列化 php,seo推广的公司,springboot出错,dede 标签,好看的手机app网站模板,网页制作模板与素材,手机登陆模板 html代码,网站后台操作流程,html纯移动端页面框架,bootstrap 管理系统页面,socket通信程序lzw
XML在线压缩/格式化工具:http://tools.jb51.net/code/xml_format_compress
XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
您可能感兴趣的文章:
PHP中类静态调用和范围解析操作符之间的区别
PHP基于数组实现的堆栈和队列功能示例详解
基于PHP7错误处理与异常处理方法详解