Chinaunix首页 | 论坛 | 博客
  • 博客访问: 16254251
  • 博文数量: 5645
  • 博客积分: 9880
  • 博客等级: 中将
  • 技术积分: 68081
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-28 13:35
文章分类

全部博文(5645)

文章存档

2008年(5645)

我的朋友

分类:

2008-04-28 14:20:14

下载本文示例代码

以下为引用的内容:
messages.xml
========================================================



     System Down for Maintenance
    Going down for maintenance soon!
    
   Joe SystemGod
   systemgod@someserver.com

    

    March 4, 2004
   10

========================================================

xml 是一种创建元数据的语言,元数据是描述其它数据的数据,PHP中的XML处理是基于LIBXML2的,安装时默认开启。

可以通过phpinfo()函数查看是否开启了XML处理模块,DOM,LIBXML,SAMPLEXML。

首先,通过samplexml_load_file函数把xml文件加载到一个对象中,samplexml_load_file可以用户远程文件。

例如:

$xml = samplexml_load_file("messages.xml"); // 本地文件系统,当前目录

$xml = samplexml_load_file("); // 远程web服务器

用 var_dump($xml) 和 print_r($xml) 分别输出其结构.var_dump给出了变量的类型和长度,而print_r可读性更强输出对象中的所有元素名称和它的值。

echo $xml->MessageTitle; //输出消息的标题

echo $xml->MessageBody; // 输出消息体

echo $xml->MessageAuthor; //消息的作者

echo $xml->MessageDate;  // 消息产生的日期

echo $xml->MessageNumber;  // 消息代码

另外,还有一个函数,可以把XML字符串加载到一个simplexml对象中取。

以下为引用的内容:

$channel =<<<_XML_

What's For Dinner

These are your choices of what to eat tonight.

_XML_;

$xml = simplexml_load_string($channel);

rss.xml

=============================================



 What's For Dinner
 
 These are your choices of what to eat tonight.
 
  Braised Sea Cucumber
  http://menu.example.com/dishes.php?dish=cuke
  Gentle flavors of the sea that nourish and refresh you.
 

 
  Baked Giblets with Salt
 
  Rich giblet flavor infused with salt and spice.
 

 
  Abalone with Marrow and Duck Feet

 
  There's no mistaking the special pleasure of abalone.
 

1、访问具有相同元素名称的节点

2、通过foreach循环所有相同元素名称的子节点

以下为引用的内容:
foreach($xml->channel->item as $key=>$value)
{
print "Title: " . $item->title . "\n";
}

3、输出整个文档

echo $xml->asXML();

4、把节点作为字符串输出

echo $xml->channel->item[0]->asXML();

这将输出文本

以下为引用的内容:

Braised Sea Cucumber
http://menu.example.com/dishes.php?dish=cuke
Gentle flavors of the sea that nourish and refresh you.

带文件名参数的asXML将会把原本输出的内容保存为一个文件

$xml->channel->item[0]->asXML("item[0].xml");

完整的代码:

以下为引用的内容:

rss.xml
=====



 What's For Dinner
 
 These are your choices of what to eat tonight.
 
  Braised Sea Cucumber
  http://menu.example.com/dishes.php?dish=cuke
  Gentle flavors of the sea that nourish and refresh you.
 

 
  Baked Giblets with Salt
 
  Rich giblet flavor infused with salt and spice.
 

 
  Abalone with Marrow and Duck Feet
 
  There's no mistaking the special pleasure of abalone.
 


rss.php
======
$xml = simplexml_load_file("rss.xml");

echo "

".$xml->channel->title."


";
echo "
    ";
    echo "
  • Title:".$xml->channel->item[0]->title."
  • ";
    echo "
  • Title:".$xml->channel->item[1]->title."
  • ";
    echo "
  • Title:".$xml->channel->item[2]->title."
  • ";
    echo "
";
print "Title: " . $xml->channel->item[0]->title . "\n
";
print "Title: " . $xml->channel->item[1]->title . "\n
";
print "Title: " . $xml->channel->item[2]->title . "\n
";
echo "
";

foreach ($xml->channel->item[0] as $element_name => $content) {
  print "The $element_name is $content\n
";
}

echo "


";
print_r($xml);
echo $xml->channel->item[0]->asXML();
?>

任何XML文本在输出前最好用 htmlentiteis() 函数编码后再输出,否这可能出现问题!

以下为引用的内容:
messages.xml
========================================================



     System Down for Maintenance
    Going down for maintenance soon!
    
   Joe SystemGod
   systemgod@someserver.com

    

    March 4, 2004
   10

========================================================

xml 是一种创建元数据的语言,元数据是描述其它数据的数据,PHP中的XML处理是基于LIBXML2的,安装时默认开启。

可以通过phpinfo()函数查看是否开启了XML处理模块,DOM,LIBXML,SAMPLEXML。

首先,通过samplexml_load_file函数把xml文件加载到一个对象中,samplexml_load_file可以用户远程文件。

例如:

$xml = samplexml_load_file("messages.xml"); // 本地文件系统,当前目录

$xml = samplexml_load_file("); // 远程web服务器

用 var_dump($xml) 和 print_r($xml) 分别输出其结构.var_dump给出了变量的类型和长度,而print_r可读性更强输出对象中的所有元素名称和它的值。

echo $xml->MessageTitle; //输出消息的标题

echo $xml->MessageBody; // 输出消息体

echo $xml->MessageAuthor; //消息的作者

echo $xml->MessageDate;  // 消息产生的日期

echo $xml->MessageNumber;  // 消息代码

另外,还有一个函数,可以把XML字符串加载到一个simplexml对象中取。

以下为引用的内容:

$channel =<<<_XML_

What's For Dinner

These are your choices of what to eat tonight.

_XML_;

$xml = simplexml_load_string($channel);

rss.xml

=============================================



 What's For Dinner
 
 These are your choices of what to eat tonight.
 
  Braised Sea Cucumber
  http://menu.example.com/dishes.php?dish=cuke
  Gentle flavors of the sea that nourish and refresh you.
 

 
  Baked Giblets with Salt
 
  Rich giblet flavor infused with salt and spice.
 

 
  Abalone with Marrow and Duck Feet

 
  There's no mistaking the special pleasure of abalone.
 

1、访问具有相同元素名称的节点

2、通过foreach循环所有相同元素名称的子节点

以下为引用的内容:
foreach($xml->channel->item as $key=>$value)
{
print "Title: " . $item->title . "\n";
}

3、输出整个文档

echo $xml->asXML();

4、把节点作为字符串输出

echo $xml->channel->item[0]->asXML();

这将输出文本

以下为引用的内容:

Braised Sea Cucumber
http://menu.example.com/dishes.php?dish=cuke
Gentle flavors of the sea that nourish and refresh you.

带文件名参数的asXML将会把原本输出的内容保存为一个文件

$xml->channel->item[0]->asXML("item[0].xml");

完整的代码:

以下为引用的内容:

rss.xml
=====



 What's For Dinner
 
 These are your choices of what to eat tonight.
 
  Braised Sea Cucumber
  http://menu.example.com/dishes.php?dish=cuke
  Gentle flavors of the sea that nourish and refresh you.
 

 
  Baked Giblets with Salt
 
  Rich giblet flavor infused with salt and spice.
 

 
  Abalone with Marrow and Duck Feet
 
  There's no mistaking the special pleasure of abalone.
 


rss.php
======
$xml = simplexml_load_file("rss.xml");

echo "

".$xml->channel->title."


";
echo "
    ";
    echo "
  • Title:".$xml->channel->item[0]->title."
  • ";
    echo "
  • Title:".$xml->channel->item[1]->title."
  • ";
    echo "
  • Title:".$xml->channel->item[2]->title."
  • ";
    echo "
";
print "Title: " . $xml->channel->item[0]->title . "\n
";
print "Title: " . $xml->channel->item[1]->title . "\n
";
print "Title: " . $xml->channel->item[2]->title . "\n
";
echo "
";

foreach ($xml->channel->item[0] as $element_name => $content) {
  print "The $element_name is $content\n
";
}

echo "


";
print_r($xml);
echo $xml->channel->item[0]->asXML();
?>

任何XML文本在输出前最好用 htmlentiteis() 函数编码后再输出,否这可能出现问题!

下载本文示例代码


用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法用SimpleXML解析XML文档的方法
阅读(313) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~