PHP解析xml格式数据工具类实例分享
本文主要介绍了PHP解析xml格式数据工具类,涉及php针对xml格式数据节点添加、获取、解析等相关操作技巧,需要的朋友可以参考下,希望能帮助到大家。
本文实例讲述了PHP解析xml格式数据工具类。分享给大家供大家参考,具体如下:
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 < $this->_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); } }
相关推荐:
以上是PHP解析xml格式数据工具类实例分享的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

创建Oracle数据库并非易事,需理解底层机制。1. 需了解数据库和Oracle DBMS的概念;2. 掌握SID、CDB(容器数据库)、PDB(可插拔数据库)等核心概念;3. 使用SQL*Plus创建CDB,再创建PDB,需指定大小、数据文件数、路径等参数;4. 高级应用需调整字符集、内存等参数,并进行性能调优;5. 需注意磁盘空间、权限和参数设置,并持续监控和优化数据库性能。 熟练掌握需不断实践,才能真正理解Oracle数据库的创建和管理。

要查看Oracle数据库,可通过SQL*Plus(使用SELECT命令)、SQL Developer(图形化界面)、或系统视图(显示数据库内部信息)。基础步骤包括连接到数据库、使用SELECT语句筛选数据,以及优化查询以提高性能。此外,系统视图提供了数据库的详细信息,有助于监控和排除故障。通过实践和持续学习,可以深入探索Oracle数据库的奥妙。

创建Oracle数据库,常用方法是使用dbca图形化工具,步骤如下:1. 使用dbca工具,设置dbName指定数据库名;2. 设置sysPassword和systemPassword为强密码;3. 设置characterSet和nationalCharacterSet为AL32UTF8;4. 设置memorySize和tablespaceSize根据实际需求调整;5. 指定logFile路径。 高级方法为使用SQL命令手动创建,但更复杂易错。 需要注意密码强度、字符集选择、表空间大小及内存

Oracle SQL语句的核心是SELECT、INSERT、UPDATE和DELETE,以及各种子句的灵活运用。理解语句背后的执行机制至关重要,如索引优化。高级用法包括子查询、连接查询、分析函数和PL/SQL。常见错误包括语法错误、性能问题和数据一致性问题。性能优化最佳实践涉及使用适当的索引、避免使用SELECT *、优化WHERE子句和使用绑定变量。掌握Oracle SQL需要实践,包括代码编写、调试、思考和理解底层机制。

Oracle 数据库登录不仅涉及用户名和密码,还包括连接字符串(包含服务器信息和凭证)以及身份验证方式。它支持 SQL*Plus 和编程语言连接器,并提供用户名密码、Kerberos 和 LDAP 等身份验证选项。常见错误包括连接字符串错误和无效的用户名/密码,而最佳实践侧重于连接池、参数化查询、索引和安全凭证处理。

PHP社区提供了丰富的资源和支持,帮助开发者成长。1)资源包括官方文档、教程、博客和开源项目如Laravel和Symfony。2)支持可以通过StackOverflow、Reddit和Slack频道获得。3)开发动态可以通过关注RFC了解。4)融入社区可以通过积极参与、贡献代码和学习分享来实现。

远程连接Oracle需配置监听器、服务名及网络。1. 客户端请求经监听器转发至数据库实例;2. 实例验证身份后建立会话;3. 使用SQL*Plus连接需指定用户名/密码、主机名、端口号及服务名,确保客户端能访问服务器且配置一致。连接失败时,检查网络连接、防火墙、监听器及用户名密码,ORA-12154错误则检查监听器和网络配置。 高效连接需使用连接池、优化SQL语句及选择合适的网络环境。

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。
