新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     >>W3CHINA.ORG讨论区<<     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论DOM, SAX, XPath等。
    [返回] W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWLXML.ORG.CN讨论区 - XML技术『 DOM/SAX/XPath 』 → [转帖]使用PHP DOM-XML创建和解析XML文件 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 5446 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [转帖]使用PHP DOM-XML创建和解析XML文件 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     Qr 帅哥哟,离线,有人找我吗?
      
      
      威望:9
      等级:博士二年级(版主)
      文章:4392
      积分:29981
      门派:XML.ORG.CN
      注册:2004/5/15

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给Qr发送一个短消息 把Qr加入好友 查看Qr的个人资料 搜索Qr在『 DOM/SAX/XPath 』的所有贴子 访问Qr的主页 引用回复这个贴子 回复这个贴子 查看Qr的博客楼主
    发贴心情 [转帖]使用PHP DOM-XML创建和解析XML文件

    <?php
    /**
    * Topic: Create and parse XML files using PHP DOM-XML
    * Source: http://www.php.net/domxml
    * Reference: http://www.zugeschaut-und-mitgebaut.de/php/extension.domxml.html
    * Author: urs@circle.ch, 16-1-2001
    *
    */
    // 使用PHP DOM-XML创建和解析XML文件
    //创建XML文档对象;以后的处理过程将在此基础上进行
    $doc = new_xmldoc("1.0" );

    //创建根节点,并设置一个属性
    $root = $doc->add_root("faq" );
    $root->setattr("page", "32" );

    //子节点
    $one = $root->new_child("question", "");
    //为子节点设置属性
    $one->setattr("number", "1");
    //question也创建子节点,并且给它赋值
    $one->new_child("text", "1. Where to get libxml-2.0.0?");
    $one->new_child("answer", "You can download the latest
    release of libxml either as a source archive or
    RPM package from http://www.xmlsoft.org.
    The current version is libxml2-2.2.1." );

    $two = $root->new_child("question", "" );
    $two->setattr("number", "2");
    $two->new_child("text", "2. How to configure PHP4?" );
    // 创建一个不直接赋值的节点
    $twoone = $two->new_child("answer", "");
    // 然后给它单独赋值
    $twoone->set_content("DIR is the libxml install directory
    (if you just use --with-dom it defaults
    to /usr), I needed to use --with-dom=/usr/local" );

    $three = $root->new_child("question", "" );
    $three->setattr("number", "7" );
    $three->new_child("text", "7. How to use DOM XML function ?" );
    $three->new_child("answer", "Read this document source for
    a simple example." );

    //输出到Browser
    print("<pre>".htmlspecialchars($doc->dumpmem() )."</pre>" );

    // write to file
    //写回到文件
    $fp = fopen("test_dom.xml", "w+" );
    fwrite($fp, $doc->dumpmem(), strlen($doc->dumpmem() ));
    fclose($fp);

    // ------------------------------------------------------
    //现在使用xpath从XML文档中得到内容

    $doc = xmldoc(join("", file("test_dom.xml")) );
    $ctx = xpath_new_context($doc );

    //所有对象
    $foo = xpath_eval($ctx, "//child::*");
    print_r($foo);
    print("<br/><br/>");
    //text node 对象
    $foo = xpath_eval($ctx, "//text");
    print_r($foo);
    print("<br/><br/>");
    // 第一个text node对象
    $foo = xpath_eval($ctx, "//text[1]");
    print_r($foo);
    print("<br/><br/>");
    // 第二个text node对象
    $foo = xpath_eval($ctx, "//text[2]");
    print_r($foo);
    print("<br/><br/>");
    // 第三个answer对象
    $foo = xpath_eval($ctx, "//answer[3]");
    print_r($foo);
    print("<br/><br/>");

    //第三个text node的类型,名称和内容
    $foo = xpath_eval($ctx, "//text[3]");
    $tmp = $foo->nodeset;
    print_r($tmp);
    print("<br/>");
    print($tmp[0]->type) . "; ";
    print($tmp[0]->name) . "; ";
    print($tmp[0]->content);

    ?>


    需要说明,PHP DOM 只能在PHP PHP4.0.x + linux上运行


    转自:http://www.iyuanma.com/info/26/25823_200592693126.htm


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    没人帮忙,那就靠自己,自己才是最好的老师!本人拒绝回答通过站内短消息提出的问题!

    blog:http://Qr.blogger.org.cn

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/11/2 9:46:00
     
     jyf1987 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:72
      门派:XML.ORG.CN
      注册:2006/12/26

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给jyf1987发送一个短消息 把jyf1987加入好友 查看jyf1987的个人资料 搜索jyf1987在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看jyf1987的博客2
    发贴心情 
    如果想把一个节点添加到最前面该怎么写?php
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/1/14 11:54:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 DOM/SAX/XPath 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/7/4 23:08:52

    本主题贴数2,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    46.875ms