***codeigniter操作xml(Simplexml第三方扩展)
This Simplexml class provides an alternative implementation of the SimpleXML API that works under PHP 4, so if you have an application that is running under PHP4 environment this is really helpful for you.
The original class was created by Taha Paksu of http://www.phpclasses.org and it was modified by Chris Brainard so that it would work with codeigniter.
Lets take a look at the code.
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Simplexml{
- var $result = array();
- var $ignore_level = 0;
- var $skip_empty_values = false;
- var $php_errormsg;
- var $evalCode="";
- function xml_parse($data){
- $php_errormsg="";
- $this->result="";
- $this->evalCode="";
- $values="";
- $encoding = 'UTF-8';
- $parser = xml_parser_create($encoding);
- xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
- xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
- $ok = xml_parse_into_struct($parser, $data, $values);
- if (!$ok) {
- $errmsg = sprintf("XML parse error %d '%s' at line %d, column %d (byte index %d)",
- xml_get_error_code($parser),
- xml_error_string(xml_get_error_code($parser)),
- xml_get_current_line_number($parser),
- xml_get_current_column_number($parser),
- xml_get_current_byte_index($parser));
- }
- xml_parser_free($parser);
- return $this->xml_reorganize($values);
- }
- function xml_reorganize($array)
- {
- $count = count($array);
- $repeat = $this->xml_tags($array);
- $repeatedone = false;
- $tags = array();
- $k = 0;
- for ($i = 0; $i < $count; $i++) {
- switch ($array[$i]['type']) {
- case 'open':
- array_push($tags, $array[$i]['tag']);
- if ($i > 0 && ($array[$i]['tag'] == $array[$i-1]['tag']) && ($array[$i-1]['type'] == 'close'))
- $k++;
- if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {
- array_push($tags, '@content');
- $this->array_insert(count($tags), $tags, $array[$i]['value'], "open");
- array_pop($tags);
- }
- if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
- if (($repeatedone == $array[$i]['tag'] . $array[$i]['level']) && ($repeatedone)) {
- array_push($tags, strval($k++));
- } else {
- $repeatedone = $array[$i]['tag'] . $array[$i]['level'];
- array_push($tags, strval($k));
- }
- }
- if (isset($array[$i]['attributes']) && $array[$i]['attributes'] && $array[$i]['level'] != $this->ignore_level) {
- array_push($tags, '@attributes');
- foreach ($array[$i]['attributes'] as $attrkey => $attr) {
- array_push($tags, $attrkey);
- $this->array_insert(count($tags), $tags, $attr, "open");
- array_pop($tags);
- }
- array_pop($tags);
- }
- break;
- case 'close':
- array_pop($tags);
- if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
- if ($repeatedone == $array[$i]['tag'] . $array[$i]['level']) {
- array_pop($tags);
- } else {
- $repeatedone = $array[$i + 1]['tag'] . $array[$i + 1]['level'];
- array_pop($tags);
- }
- }
- break;
- case 'complete':
- array_push($tags, $array[$i]['tag']);
- if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
- if ($repeatedone == $array[$i]['tag'] . $array[$i]['level'] && $repeatedone) {
- array_push($tags, strval($k));
- } else {
- $repeatedone = $array[$i]['tag'] . $array[$i]['level'];
- array_push($tags, strval($k));
- }
- }
- if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {
- if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {
- array_push($tags, '@content');
- $this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");
- array_pop($tags);
- } else {
- $this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");
- }
- }
- if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {
- array_push($tags, '@attributes');
- foreach ($array[$i]['attributes'] as $attrkey => $attr) {
- array_push($tags, $attrkey);
- $this->array_insert(count($tags), $tags, $attr, "complete");
- array_pop($tags);
- }
- array_pop($tags);
- }
- if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
- array_pop($tags);
- $k++;
- }
- array_pop($tags);
- break;
- }
- }
- eval($this->evalCode);
- $last = $this->array_reindex($this->result);
- return $last;
- }
- function array_insert($level, $tags, $value, $type)
- {
- $temp = '';
- for ($c = $this->ignore_level + 1; $c < $level + 1; $c++) {
- if (isset($tags[$c]) && (is_numeric(trim($tags[$c])) || trim($tags[$c]))) {
- if (is_numeric($tags[$c])) {
- $temp .= '[' . $tags[$c] . ']';
- } else {
- $temp .= '["' . $tags[$c] . '"]';
- }
- }
- }
- $this->evalCode .= '$this->result' . $temp . "=\"" . addslashes($value) . "\";//(" . $type . ")\n";
- #echo $code. "\n";
- }
- /**
- * Define the repeated tags in XML file so we can set an index
- *
- * @param array $array
- * @return array
- */
- function xml_tags($array)
- { $repeats_temp = array();
- $repeats_count = array();
- $repeats = array();
- if (is_array($array)) {
- $n = count($array) - 1;
- for ($i = 0; $i < $n; $i++) {
- $idn = $array[$i]['tag'].$array[$i]['level'];
- if(in_array($idn,$repeats_temp)){
- $repeats_count[array_search($idn,$repeats_temp)]+=1;
- }else{
- array_push($repeats_temp,$idn);
- $repeats_count[array_search($idn,$repeats_temp)]=1;
- }
- }
- }
- $n = count($repeats_count);
- for($i=0;$i<$n;$i++){
- if($repeats_count[$i]>1){
- array_push($repeats,$repeats_temp[$i]);
- }
- }
- unset($repeats_temp);
- unset($repeats_count);
- return array_unique($repeats);
- }
- /**
- * Converts Array Variable to Object Variable
- *
- * @param array $arg_array
- * @return $tmp
- */
- function array2object ($arg_array)
- {
- if (is_array($arg_array)) {
- $keys = array_keys($arg_array);
- if(!is_numeric($keys[0])) $tmp = new Xml;
- foreach ($keys as $key) {
- if (is_numeric($key)) $has_number = true;
- if (is_string($key)) $has_string = true;
- }
- if (isset($has_number) and !isset($has_string)) {
- foreach ($arg_array as $key => $value) {
- $tmp[] = $this->array2object($value);
- }
- } elseif (isset($has_string)) {
- foreach ($arg_array as $key => $value) {
- if (is_string($key))
- $tmp->$key = $this->array2object($value);
- }
- }
- } elseif (is_object($arg_array)) {
- foreach ($arg_array as $key => $value) {
- if (is_array($value) or is_object($value))
- $tmp->$key = $this->array2object($value);
- else
- $tmp->$key = $value;
- }
- } else {
- $tmp = $arg_array;
- }
- return $tmp; //return the object
- }
- /**
- * Reindexes the whole array with ascending numbers
- *
- * @param array $array
- * @return array
- */
- function array_reindex($array)
- {
- if (is_array($array)) {
- if(count($array) == 1 && $array[0]){
- return $this->array_reindex($array[0]);
- }else{
- foreach($array as $keys => $items) {
- if (is_array($items)) {
- if (is_numeric($keys)) {
- $array[$keys] = $this->array_reindex($items);
- } else {
- $array[$keys] = $this->array_reindex(array_merge(array(), $items));
- }
- }
- }
- }
- }
- return $array;
- }
- }
In this article we don’t need to understand what is in the code above. What is my aim is to show you how to use it.
Supposing you have an xml format like below, and you need it to present as a tabular data in an html page.
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <item>
- <id>1</id>
- <name>iPhone</name>
- <category>Mobile</category>
- <price>300</price>
- </item>
- <item>
- <id>2</id>
- <name>iMac</name>
- <category>Desktop Computers</category>
- <price>2500</price>
- </item>
- <item>
- <id>3</id>
- <name>MacBook Pro</name>
- <category>Mobile PC</category>
- <price>2000</price>
- </item>
- <item>
- <id>4</id>
- <name>iTouch</name>
- <category>Gadgets</category>
- <price>150</price>
- </item>
- <item>
- <id>5</id>
- <name>Wii</name>
- <category>Gaming</category>
- <price>1250</price>
- </item>
- <item>
- <id>6</id>
- <name>Time Capsule</name>
- <category>Acessories</category>
- <price>1000</price>
- </item>
- <item>
- <id>7</id>
- <name>Apple TV</name>
- <category>Gadgets</category>
- <price>800</price>
- </item>
- </products>
Lets start by creating our controller, to make it easier lets make use of the default controller in fresh CI installation which is the welcome controller. Now open you welcome controller and in you index function populate the code below.
- function index()
- {
- //load the parser library
- $this->load->library('parser');
- $data['title'] = 'Parsing XML using Simplexml class of CodeIgniter';
- $data['products'] = $this->_getXML('myxml');
- $this->parser->parse('table_view', $data);
- }
In this function we load the parser library, for those who dont know what a parser library is, its a simple templating engine of codeIgniter. Im using this almost always for me to get rid if the php tag in my view. Next we have a varialble title
, and a variable products
which calls the _getXML
function with a string parametermyxml
, we use this as reference of the filename for our XML file to be parse. Then load our table_view
.
Lets add the _getXML
function to our controller. Add this code in your welcome controller.
- function _getXML($fname)
- {
- $filename = $fname.'.xml';
- $xmlfile="./xml/".$filename;
- $xmlRaw = file_get_contents($xmlfile);
- $this->load->library('simplexml');
- $xmlData = $this->simplexml->xml_parse($xmlRaw);
- foreach($xmlData['item'] as $row)
- {
- $result .= '<tr>';
- $result .= '<td>'.$row['id'].'</td>';
- $result .= '<td>'.$row['name'].'</td>';
- $result .= '<td>'.$row['category'].'</td>';
- $result .= '<td>$ '.$row['price'].'</td>';
- $result .= '</tr>';
- }
- return $result;
- }
This assume that the file location of your xml file is in the root of your CI installation an inside the xml
folder. Then using the file_get_contents()
function we load the xml data to $xmlRaw
varialble. We loaded the simple XML library and then we populate it to the table element using foreach()
function.
Now you only need to add a very little code in your view file.
- <table cellpadding="0" cellspacing="0">
- <thead>
- <th>
- <td>PRODUCT ID</td>
- <td>PRODUCT NAME</td>
- <td>CATEGORY</td>
- <td>PRICE</td>
- </th>
- </thead>
- <tbody>
- {products}
- </tbody>
- </table>
Thats it!. Adding some quick css styling and a jQuery for table row stripe effect. You get something like this.
Adding a quick table stripe effect.
Download and include the jQuery library file in your view.
- <script type="text/javascript" src="public/js/jquery.js"></script>
Then add this line before the tag in your view file.
- <script type="text/javascript">
- $(document).ready(function(){
- $("table tr:nth-child(even)").addClass("even");
- });
- </script>
And you must have a style like this in your css file.
- table tr.even td{
- background: #cdded6;
- }
Were done. Thanks for reading.
--------------------------------------------------------------链接:
http://justcoding.iteye.com/blog/558775
http://blog.insicdesigns.com/2009/03/parsing-xml-file-using-codeigniters-simplexml-library/
BUG修改:
Q:
nice work and really working fine please can you update this library because i have seen in comments
if(count($array) == 1 && isset($array[0]))
please upate it.
A:
change this line if(count($array) == 1 && $array[0]){
to below line..this line exist in library function is "array_reindex" (line no 230)
if(count($array) == 1 && array_key_exists(0, $array)){
***codeigniter操作xml(Simplexml第三方扩展)的更多相关文章
- C#操作Xml树的扩展类
本文提供一个操作Xml树的扩展类,与将xml字符串直接映射成实体对象的使用方法,供大家参考,学习. 下面附上源码 using System; using System.Collections.Gene ...
- PHP操作XML方法之SimpleXML
SimpleXML简介 SimpleXML 扩展提供了一个非常简单和易于使用的工具集,能将XML转换成一个带有一般属性选择器和数组迭代器的对象. 举例XML XML结构部分引用自<<深入理 ...
- php simpleXML操作xml的用法
XML简介 XML是一种流行的半结构化文件格式,以一种类似数据库的格式存储数据.在实际应用中,一些简单的.安全性较低的数据往往使用 XML文件的格式进行存储.这样做的好处一方面可以通过减少与数据库的交 ...
- XMl入门介绍及php操作XML
一.什么是XML XML全称:Extensible Markup Language 中文名:可扩展标记语言 用于标记电子文件使其具有结构性的标记语言,可以用来标记数据,定义数据类型,允许用户对自己的标 ...
- delphi操作xml学习笔记 之一 入门必读
Delphi 对XML的支持---TXMLDocument类 Delphi7 支持对XML文档的操作,可以通过TXMLDocument类来实现对XML文档的读写.可以利用TXMLDocum ...
- php : DOM 操作 XML
DOM 操作 XML 基本用法 XML文件: person.XML <?xml version="1.0" encoding="utf-8" ?> ...
- php操作xml
最近计划写个人的小网站,一系列原因选择了用php来写,最大的问题就是虽然php很流行,但我从来没有接触过php,看了一个多星期的基本语法后做些小练习热热身,但是期间是各种问题啊,主要是对php不熟悉, ...
- 让时间处理简单化 【第三方扩展类库org.apache.commons.lang.time】
JAVA的时间日期处理一直是一个比较复杂的问题,大多数程序员都不能很轻松的来处理这些问题.首先Java中关于时间的类,从 JDK 1.1 开始,Date的作用很有限,相应的功能已由Calendar与D ...
- Delphi操作XML简介
参考:http://www.delphifans.com/InfoView/Article_850.html Delphi 7支持对XML文档的操作,可以通过 TXMLDocument类来实现对XML ...
随机推荐
- web项目中的执行流程参数传递详解
还是从这个图开始讲解: struts2中有一个存放数据的中心:值栈.(值栈里面有map和对象栈) 首先:值栈的作用范围是一个请求:request作用域(一个请求是代表的一个过程,即页面点击到数据返回到 ...
- mysql快速导入5000万条数据过程记录(LOAD DATA INFILE方式)
mysql快速导入5000万条数据过程记录(LOAD DATA INFILE方式) 首先将要导入的数据文件top5000W.txt放入到数据库数据目录/var/local/mysql/data/${d ...
- Rstudio常用快捷键
多行注释 ctrl+shift+c 运行单行或选中代码 ctrl+enter 查看帮助 F1
- tips 前端 bootstrap 嵌套行 嵌套列 溢出 宽度不正确 栅格化系统计算
bootstrap 当嵌套列时 有时会出现很奇异的row 的width不对问题出现的情况时 <div class="row" > <!--row a--> ...
- Jquery Ajax自定义无刷新提交表单Form
Jquery的$.ajax方法可以实现ajax调用,要设置url,post,参数等. 如果要提交现有Form需要写很多代码,何不直接将Form的提交直接转移到ajax中呢. 以前的处理方法 如Form ...
- 20155223 2016-2017-2 《Java程序设计》第7周学习总结
20155223 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 第十二章 Lambdo表达式下,任何参数的类型必须标明清楚:如果有目标类型的话,在编译程序可 ...
- 20155331 2016-2017-2 《Java程序设计》第6周学习总结
20155331 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 输入/输出基础 很多实际的Java应用程序不是基于文本的控制台程序.尽管基于文本的程序作为教 ...
- SQL查找数据库中所有没有主键的数据表脚本
--SQL查找数据库中所有没有主键的数据表脚本 --运行脚本后在消息中可能会显示下面现象中的一种:--(1)数据库中所有数据表都有主键(则证明所有数据表都有主键)--(2)当前数据表[数据表名]没有主 ...
- C 语言中指针初始化为字符串常量 不可通过该指针修改其内容
char b[] = "hello"; 则“hello”存于栈中,因为定义的是一个数组. char *b = "hello"; 则"hello&quo ...
- CF448C Painting Fence
传送门 Descriptionzed 最近总是受到 Farmer 的困扰,因此他在自家的门前插了一排栅栏以防农气的入侵.栅栏由 N 个竖条栅栏横向组成,每个竖条栅栏宽度为 1.过了一段时间,zed 觉 ...