|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
我的文章不会对您的学习起到实质性的作用,您能否成功,还得靠自己的,坚持,坚持,再坚持,就是步入成功的不二法门。php5|xml messages.xml
========================================================
<?xml version="1.0" ?>
<!--Sample XML document -->
<SystemMessage>
<MessageTitle>System Down for Maintenance</MessageTitle>
<MessageBody>Going down for maintenance soon!</MessageBody>
<MessageAuthor>
<MessageAuthorName>Joe SystemGod</MessageAuthorName>
<MessageAuthorEmail>systemgod@someserver.com</MessageAuthorEmail>
</MessageAuthor>
<MessageDate>March 4, 2004</MessageDate>
<MessageNumber>10</MessageNumber>
</SystemMessage>========================================================
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("http://www.xml.org.cn/messages.xml"); // 近程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_
<channel>
<title>What's For Dinner</title>
<link>http://menu.example.com/</link>
<description>These are your choices of what to eat tonight. </description>
</channel>
_XML_;
$xml = simplexml_load_string($channel);
===================================================
rss.xml
=============================================
<?xml version="1.0" encoding="utf-8"?>
<rss version="0.91">
<channel>
<title>What's For Dinner</title>
<link>http://menu.example.com/</link>
<description>These are your choices of what to eat tonight.</description>
<item>
<title>Braised Sea Cucumber</title>
<link>http://menu.example.com/dishes.php?dish=cuke</link>
<description>Gentle flavors of the sea that nourish and refresh you. </description>
</item>
<item>
<title>Baked Giblets with Salt</title>
<link>http://menu.example.com/dishes.php?dish=giblets</link>
<description>Rich giblet flavor infused with salt and spice. </description>
</item>
<item>
<title>Abalone with Marrow and Duck Feet</title>
<link>http://menu.example.com/dishes.php?dish=abalone</link>
<description>There's no mistaking the special pleasure of abalone. </description>
</item>
</channel>
</rss>
=====================================================
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();
这将输入文本
<item>
<title>Braised Sea Cucumber</title>
<link>http://menu.example.com/dishes.php?dish=cuke</link>
<description>Gentle flavors of the sea that nourish and refresh you. </description>
</item>
带文件名参数的asXML将会把本来输入的内容保留为一个文件
$xml->channel->item[0]->asXML("item[0].xml");
完全的代码:
rss.xml
=====
<?xml version="1.0" encoding="utf-8"?>
<rss version="0.91">
<channel>
<title>What's For Dinner</title>
<link>http://menu.example.com/</link>
<description>These are your choices of what to eat tonight.</description>
<item>
<title>Braised Sea Cucumber</title>
<link>http://menu.example.com/dishes.php?dish=cuke</link>
<description>Gentle flavors of the sea that nourish and refresh you. </description>
</item>
<item>
<title>Baked Giblets with Salt</title>
<link>http://menu.example.com/dishes.php?dish=giblets</link>
<description>Rich giblet flavor infused with salt and spice. </description>
</item>
<item>
<title>Abalone with Marrow and Duck Feet</title>
<link>http://menu.example.com/dishes.php?dish=abalone</link>
<description>There's no mistaking the special pleasure of abalone. </description>
</item>
</channel>
</rss>
rss.php
======
<?php
$xml = simplexml_load_file("rss.xml");
echo "<h3>".$xml->channel->title."</h3><br>";
echo "<ul>";
echo "<li>Title:".$xml->channel->item[0]->title."</li>";
echo "<li>Title:".$xml->channel->item[1]->title."</li>";
echo "<li>Title:".$xml->channel->item[2]->title."</li>";
echo "</ul>";
print "Title: " . $xml->channel->item[0]->title . "\n<br>";
print "Title: " . $xml->channel->item[1]->title . "\n<br>";
print "Title: " . $xml->channel->item[2]->title . "\n<br>";
echo "<hr>";
foreach ($xml->channel->item[0] as $element_name => $content) {
print "The $element_name is $content\n<br>";
}
echo "<hr>";
print_r($xml);
echo $xml->channel->item[0]->asXML();
?>
任何XML文本在输入前最好用 htmlentiteis() 函数编码后再输入,否这能够呈现成绩
聪明的你,显然已经逐渐的开悟了,慢慢的理解了编程的概念,那么祝贺你,你已经迈出了成功的第一步。 |
|