1.生成XML为字符串
header('Content-Type: text/xml');
print '<?xml version="1.0">'."\n";
print '<shows>';
$shows = array(array('name' => 'Simpsons',
'channel' => 'FOX',
'start' => '8:00PM',
'duration' => '30'),
array('name' => 'Law & Order',
'channel' => 'NBC',
'start' => '8:00PM',
'duration' => '60'));
foreach ($shows as $show) {
print "<show>\n";
foreach ($show as $tag => $data) {
print "<$tag>".htmlspecialchars($data)."</$tag>\n";
}
print "</show>\n";
}
print "<shows>\n";
首先需要header()为该文档设置正确的Content-Type,还有必须对实体进行转义
2.通过DOM生成XML:通过DOM扩展来创建一个DOMDocument对象
// 创建一个新文档
$dom = new DOMDocument('1.0');
// 创建根元素,<book>,并将其添加到文档
$book = $dom->appendChild($dom->createElement('book'));
// 创建一个title元素并将其添加到book中
$title = $book->appendChild($dom->createElement('title'));
$title->appendChild($dom->createTextNode('PHP Cookbook'));
$title->setAttribute('cover', 'soft');
$dom->formatOutput =true;
echo $dom->saveXML();
3.解析基本的XML文档:使用simplexml
$ab = simplexml_load_file('address.xml');
foreach ($ab->person as $person) {
$firstname = $person->firstname;
$lastname = $person->lastname;
echo "$firstname $lastname\n";
}
4.解析更复杂的XML文档:使用DOM扩展
$dom = new DOMDocument;
$dom->load('address.xml');
foreach ($dom->getElementsByTagName('person') as $person) {
$firstname = $person->getElementsByTagName('firstname');
$firstname_text_value = $firstname->item(0)->firstchild->nodevalue;
$lastname = $person->getElementsByTagName('lastname');
$lastname_text_value = $lastname->item(0)->lastchild->nodevalue;
print "$firstname_text_value $lastname_text_value\n";
}
5.解析大型XML文档:使用XMLReader
6.用XPath来提取信息
$s = simplexml_load_file('address.xml');
$people = $s->xpath('address-book/person');
foreach ($people as $p) {
list($firstname) = $p->xpath('firstname');
list($lastname) = $p->xpath('lastname');
print "$firstname $lastname\n";
}
7.通过XSLT转换XML:使用XSLT扩展
// 加载 XSL 模板
$xsl = new DOMDocument();
$xsl->load('stylesheet.xsl');
// 创建一个新的XSLTProcessor对象
$xslt = new XSLTProcessor();
// 加载样式表
$xslt->importStylesheet($xsl);
// 加载 XML 文档
$xml = new DOMDocument();
$xml->load('data.xml');
// 转换成字符串
$results = $xslt->transformToXml($xml);
// 转换成一个文件
$results = $xslt->transformToUri();
// 转换成DOM对象
$results = $xslt->transformToDoc();
8.在PHP中设置XSLT参数:使用XSLTProcessor::setParameter()方法
9.在XSLT样式表中调用PHP函数:调用XSLTProcessor::registerPHPFunctions()方法来实现
10.使用内容编码:在将其传递给XML扩展之前使用iconv库进行转换
11.读取RSS和Atom源:使用MagpieRSS解析器
require('rss_fetch.inc');
$feed = 'http://news.php.net/group.php?group=php.announce&format=rss';
$rss = fetch_rss($feed);
print "<ul>\n";
foreach ($rss->items as $item) {
print '<li><a href="'.$item['link'] . '">'.$item['title']."</a></li>\n";
}
print "</ul>\n";
12.生成RSS源:根据自己的数据生成RSS源,以便对你的内容整合
class rss2 extends DOMDocument {
private $channel;
public function __construct($title, $link, $description) {
parent::__construct();
$this->formatOutput = true;
$root = $this->appendChild($this->createElement('rss'));
$root->setAttribute('version', '2.0');
$channel = $root->appendChild($this->createElement('channel'));
$channel->appendChild($this->createElement('title', $title));
$channel->appendChild($this->createElement('link', $link));
$channel->appendChild($this->createElement('description', $description));
$this->channel = $channel;
}
public function addItem($title, $link, $description) {
$item = $this->createElement('item');
$item->appendChild($this->createElement('title', $title));
$item->appendChild($this->createElement('link', $link));
$item->appendChild($this->createElement('description', $description));
$this->channel->appendChild($item);
}
}
$rss = new rss2('Channel Title', 'http://www.zhangmin.com', 'Channel Description');
$rss->addItem('Item1', 'http://www.me.com', 'desc');
$rss->addItem('Item1', 'http://www.me.com', 'desc');
print $rss->saveXML();