php dom xml解析,使用PHP DOM-XML创建和解析XML文件

使用PHP DOM-XML创建和解析XML文件

更新时间:2006年10月11日 00:00:00   作者:

使用PHP DOM-XML创建和解析XML文件

/**

* 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("

".htmlspecialchars($doc->dumpmem() )."
" );

// 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("
");

//text node 对象

$foo = xpath_eval($ctx, "//text");

print_r($foo);

print("
");

// 第一个text node对象

$foo = xpath_eval($ctx, "//text[1]");

print_r($foo);

print("
");

// 第二个text node对象

$foo = xpath_eval($ctx, "//text[2]");

print_r($foo);

print("
");

// 第三个answer对象

$foo = xpath_eval($ctx, "//answer[3]");

print_r($foo);

print("
");

//第三个text node的类型,名称和内容

$foo = xpath_eval($ctx, "//text[3]");

$tmp = $foo->nodeset;

print_r($tmp);

print("
");

print($tmp[0]->type) . "; ";

print($tmp[0]->name) . "; ";

print($tmp[0]->content);

?>

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

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

相关文章

1a1b05c64693fbf380aa1344a7812747.png

这篇文章主要介绍了Xpath语法格式总结的相关内容,比较全面,这里分享给大家,如有不足之处欢迎补充。2017-10-10

4f55910a645b073bc4fc65dc10dc14bd.png

XML相关技术资料...2006-10-10

0ea3c7666119d5615e582f823fb3fad6.png

XML轻松学习手册(5)XML实例解析...2006-10-10

4f96a78db829b1556ff16de21e013c7a.png

XML轻松学习手册(五):XML语法...2006-10-10

8cc1031babc6aff2319f1c6af8544aa0.png

XML、DataSet、DataGrid结合一...2006-10-10

0c932a99bb7b6f23c937db507070cc7b.png

XSL简明教程(4)在服务器端的实现...2006-10-10

cca732bf65a93ed2ec0ac80c638460fe.png

XSLT轻松入门第一章:XSLT的概念...2006-10-10

2d9f31f2af7b675a3d153d2b7f1035a7.png

用XML和XSL来生成动态页面...2006-10-10

b452cee8ec5cd9e58ab98eba17281e59.png

了解WEB页面工具语言XML(二)定义...2006-10-10

f4838ec7e2d4da28e0b57d4e852dadd4.png

给XML文档添加新 ”records”...2006-08-08

最新评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值