首 页 教育新闻课件中心论文中心教学教案试题中心语文专题综合下载技术教程公务员  
设为首页
加入收藏
联系我们
您当前的位置:中国教育资源网 -> 技术教程 -> 网络相关 -> 服务器 -> Web服务器 -> 技术内容 退出登录 用户管理

使用PHPDOM-XML创建和解析XML文件Web服务器教程

论文作者:佚名  论文来源:不详  论文发布时间:2006-6-18 22:56:07  论文发布人:chjchjchj

减小字体 增大字体

              摘要:使用PHPDOM-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上运行


PHPDOM类库请到www.zend.com/download下

[] [返回上一页] [打 印] [收 藏]  
 ∷相关技术评论  (评论内容只代表网友观点,与本站立场无关!) [查看发表评论...]
 
 中国教育资源网免费技术教程下载中心-站内广告 站内广告 中国教育资源网免费技术教程下载中心-站内广告 
 中国教育资源网站内搜索 站内搜索 中国教育资源网站内搜索 
 

   
 中国教育资源网免费技术教程下载中心-栏目导航 栏目导航 中国教育资源网免费技术教程下载中心-栏目导航 
· Windows 9XMEXP · Windows NT20002003
· LinuxBSD · 系统综合
· IISApache · 硬件技术
· Web服务器 · FTP服务器
· 邮件服务器 · 域名服务器
· Windows服务器 · 代理服务器
· 服务器综合
 
中国教育资源网免费技术教程下载中心-相关教程  相关技术 中国教育资源网免费技术教程下载中心-相关教程
· 使用ASP生成HTML文件
· 在html文件引入其它
· [组图] 使用Photoshop画笔做
· HTML文件HEAD内部标
· 在Visual Basic 6.0
· 将指定的asp文件内容
· XML文件导入SQL Ser
· 将XML文件导入SQL S
· SQL Server根据查询
· 从SQL中的一个表中导
 中国教育资源网免费技术教程下载中心-本月热门教程 本月热门 中国教育资源网免费技术教程下载中心-本月热门教程 
 
 中国教育资源网免费技术教程下载中心-本日热门论文 本日热门 中国教育资源网免费技术教程下载中心-本日热门论文 
 
关于本站 - 网站帮助 - 免费课件 - 美容 - 绿色软件 - 软件下载 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 网站留言
浙ICP备06010405号 Email:cnkjz@163.com 技术支持:名流设计
版权所有 Copyright© 2002-2004 名流