仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1311|回复: 19
打印 上一主题 下一主题

[学习教程] PHP网页设计明天看到一个很好的类-操作xml的!贴出...

[复制链接]
蒙在股里 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:24:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
看看西,人家这个编论坛,那个CMS,还有那啥CRM,我啥时候写一个呢?xml   <?
/*
    (c) 2000 Hans Anderson Corporation.  All Rights Reserved.
    You are free to use and modify this class under the same
    guidelines found in the PHP License.

    -----------

    bugs/me:
    http://www.hansanderson.com/php/
    me@hansanderson.com
    showstv@163.com

    -----------

    Version 1.0

        - 1.0 is the first actual release of the class.  It's  
          finally what I was hoping it would be, though there
          are likely to still be some bugs in it.  This is
          a much changed version, and if you have downloaded
          a previous version, this WON'T work with your existing
          scripts!  You'll need to make some SIMPLE changes.

        - .92 fixed bug that didn't include tag attributes

          (to use attributes, add _attributes[array_index]
           to the end of the tag in question:
            $xml_html_head_body_img would become
            $xml_html_head_body_img_attributes[0],  
           for example)

           -- Thanks to Nick Winfield <nick@wirestation.co.uk>
              for reporting this bug.

        - .91 No Longer requires PHP4!

        - .91 now all elements are array.  Using objects has
          been discontinued.
*/

class xml_container{

    function store($k,$v) {
        $this->{$k}[] = $v;
    }
}


/* parses the information */
/*********************************
*    类界说入手下手
*
*********************************/
class xml{
   
    // initialize some variables
    var $current_tag=array();
    var $xml_parser;
    var $Version = 1.0;
    var $tagtracker = array();

    /* Here are the XML functions needed by expat */


    /* when expat hits an opening tag, it fires up this function */
    function startElement($parser, $name, $attrs){

        array_push($this->current_tag, $name); // add tag to the cur. tag array
        $curtag = implode("_",$this->current_tag); // piece together tag

        /* this tracks what array index we are on for this tag */

        if(isset($this->tagtracker["$curtag"])) {
            $this->tagtracker["$curtag"]++;
        }
        else{
            $this->tagtracker["$curtag"]=0;
        }

        /* if there are attributes for this tag, we set them here. */

        if(count($attrs)>0) {
            $j = $this->tagtracker["$curtag"];
            if(!$j) $j = 0;

            if(!is_object($GLOBALS[$this->identifier]["$curtag"][$j])) {
                $GLOBALS[$this->identifier]["$curtag"][$j] = new xml_container;
            }

            $GLOBALS[$this->identifier]["$curtag"][$j]->store("attributes",$attrs);
        }
   
    }// end function startElement



    /* when expat hits a closing tag, it fires up this function */
    function endElement($parser, $name) {
        $curtag = implode("_",$this->current_tag); // piece together tag
        
        // before we pop it off,
        // so we can get the correct
        // cdata

        if(!$this->tagdata["$curtag"]) {
            $popped = array_pop($this->current_tag); // or else we screw up where we are
            return; // if we have no data for the tag
        }
        else{
            $TD = $this->tagdata["$curtag"];
            unset($this->tagdata["$curtag"]);
        }

        $popped = array_pop($this->current_tag);
        // we want the tag name for
        // the tag above this, it  
        // allows us to group the
        // tags together in a more
        // intuitive way.

        if(sizeof($this->current_tag) == 0) return; // if we aren't in a tag

        $curtag = implode("_",$this->current_tag); // piece together tag
        // this time for the arrays

        $j = $this->tagtracker["$curtag"];
        
        if(!$j) $j = 0;

        if(!is_object($GLOBALS[$this->identifier]["$curtag"][$j])) {
            $GLOBALS[$this->identifier]["$curtag"][$j] = new xml_container;
        }

        $GLOBALS[$this->identifier]["$curtag"][$j]->store($name,$TD);
        #$this->tagdata["$curtag"]);
        unset($TD);
        return TRUE;
    } // end function endElement


    /* when expat finds some internal tag character data,
       it fires up this function */

    function characterData($parser, $cdata) {
        $curtag = implode("_",$this->current_tag); // piece together tag
        $this->tagdata["$curtag"] .= $cdata;
    }


    function xml($data,$identifier='xml') {   

        $this->identifier = $identifier;

        // create parser object
        $this->xml_parser = xml_parser_create();

        // set up some options and handlers
        xml_set_object($this->xml_parser,$this);
        xml_parser_set_option($this->xml_parser,XML_OPTION_CASE_FOLDING,0);
        xml_set_element_handler($this->xml_parser, "startElement", "endElement");
        xml_set_character_data_handler($this->xml_parser, "characterData");

        if (!xml_parse($this->xml_parser, $data, TRUE)) {
            sprintf("XML error: %s at line %d",
            xml_error_string(xml_get_error_code($this->xml_parser)),
            xml_get_current_line_number($this->xml_parser));
        }

        // we are done with the parser, so let's free it
        xml_parser_free($this->xml_parser);

    }//end constructor: function xml()


}//thus, we end our class xml

?>




操作办法:

require('class.xml.php');
$file = "data.xml";
$data = implode("",file($file)) or die("could not open XML input file");
$obj = new xml($data,"xml");


print $xml["hans"][0]->num_results[0];
for($i=0;$i<sizeof($xml["hans"]);$i++) {
print $xml["hans"][$i]->tag[0] . " ";
}

To print url attributes (if they exist):

print $xml["hans"][0]->attributes[0]["size"];

  虽说不上很好,但至少一般的数据操作,再在原有的SQL语言的基础上,用得还是可以的。
飘飘悠悠 该用户已被删除
沙发
发表于 2015-2-4 12:56:15 | 只看该作者
对于初学者来说不推荐去拿钱买的。当然如果一个网站你经常去用,而且里面的资料也比较有用,最好还是买个会员比较好,毕竟那些也是别人的工作成果。
灵魂腐蚀 该用户已被删除
板凳
发表于 2015-2-9 22:44:59 | 只看该作者
为了以后维护的方便最好是代码上都加上注释,“予人方便,自己方便”。此外开发文档什么的最好都弄齐全。我觉得这是程序员必备的素质。虽然会消耗点很多的时间。但是确实是非常有必要的。
金色的骷髅 该用户已被删除
地板
发表于 2015-2-11 01:09:09 | 只看该作者
这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己
冷月葬花魂 该用户已被删除
5#
发表于 2015-3-1 19:09:12 | 只看该作者
如果你已经到这种程度了,那么你已经可以做我的老师了。其实php也分很多的区域,
小魔女 该用户已被删除
6#
发表于 2015-3-4 02:12:49 | 只看该作者
微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox  支持的不是很好,所以能少用还是少用的好。
第二个灵魂 该用户已被删除
7#
发表于 2015-3-11 04:46:58 | 只看该作者
首先声明:我是一个菜鸟,是一个初学者。学习了一段php后总是感觉自己没有提高,无奈。经过反思我认为我学习过程中存在很多问题,我改变了学习方法后自我感觉有了明显的进步。
蒙在股里 该用户已被删除
8#
 楼主| 发表于 2015-3-16 19:24:09 | 只看该作者
个人呢觉得,配wamp 最容易漏的一步就是忘了把$PHP$目录下的libmysql.dll拷贝到windows系统目录的system32目录下,还有重启apache。
谁可相欹 该用户已被删除
9#
发表于 2015-3-22 23:47:15 | 只看该作者
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
不帅 该用户已被删除
10#
发表于 2015-4-19 09:00:04 | 只看该作者
说点我烦的低级错误吧,曾经有次插入mysql的时间 弄了300年结果老报错,其实mysql的时间是有限制的,大概是到203X年  具体的记不清啦,囧。
莫相离 该用户已被删除
11#
发表于 2015-4-26 08:11:31 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
深爱那片海 该用户已被删除
12#
发表于 2015-4-26 15:27:45 | 只看该作者
实践是检验自己会不会的真理。
爱飞 该用户已被删除
13#
发表于 2015-4-30 13:56:34 | 只看该作者
在我安装pear包的时候老是提示,缺少某某文件,才发现 那群extension 的排列是应该有一点的顺序,而我安装的版本的排序不是正常的排序。没办法我只好把那群冒号加了上去,只留下我需要使用的扩展。
透明 该用户已被删除
14#
发表于 2015-6-16 04:18:46 | 只看该作者
使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。
乐观 该用户已被删除
15#
发表于 2015-6-18 21:01:44 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
再现理想 该用户已被删除
16#
发表于 2015-6-19 22:03:39 | 只看该作者
实践是检验自己会不会的真理。
活着的死人 该用户已被删除
17#
发表于 2015-7-5 17:46:27 | 只看该作者
,熟悉html,能用div+css,还有javascript,优先考虑linux。我在开始学习的时候,就想把这些知识一起学习,我天真的认为同时学习能够互相呼应,因为知识是相通的。
因胸联盟 该用户已被删除
18#
发表于 2015-7-13 07:09:15 | 只看该作者
说php的话,首先得提一下数组,开始的时候我是最烦数组的,总是被弄的晕头转向,不过后来呢,我觉得数组里php里最强大的存储方法,所以建议新手们要学好数组。
小妖女 该用户已被删除
19#
发表于 2015-7-13 21:29:01 | 只看该作者
为了以后维护的方便最好是代码上都加上注释,“予人方便,自己方便”。此外开发文档什么的最好都弄齐全。我觉得这是程序员必备的素质。虽然会消耗点很多的时间。但是确实是非常有必要的。
变相怪杰 该用户已被删除
20#
发表于 2015-7-14 07:45:00 | 只看该作者
找到的的资料很多都是在论坛里的,需要注册,所以我一般没到一个论坛都注册一个id,所有的id都注册成一样的,这样下次再进来的时候就不用重复注册啦。当然有些论坛的某些资料是需要的付费的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-3 22:26

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表