使用PHP DOM-XML創(chuàng)建和解析XML文件_Xml教程
推薦:關(guān)于XML在語音合成中的應(yīng)用互聯(lián)網(wǎng)以及和它相關(guān)的一切現(xiàn)在似乎隨處可見。您也許已經(jīng)試過接到夜間電話推銷員的語音電話、又或者曾經(jīng)接到過當?shù)厮幍杲o您的處方通知。現(xiàn)在,有一種新技術(shù)可以使用語音合成結(jié)合XML技術(shù)傳送
使用PHP DOM-XML創(chuàng)建和解析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:[email protected], 16-1-2001
*
*/
// 使用PHP DOM-XML創(chuàng)建和解析XML文件
//創(chuàng)建XML文檔對象;以后的處理過程將在此基礎(chǔ)上進行
$doc = new_xmldoc("1.0" );
//創(chuàng)建根節(jié)點,并設(shè)置一個屬性
$root = $doc->add_root("faq" );
$root->setattr("page", "32" );
//子節(jié)點
$one = $root->new_child("question", "");
//為子節(jié)點設(shè)置屬性
$one->setattr("number", "1");
//question也創(chuàng)建子節(jié)點,并且給它賦值
$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?" );
// 創(chuàng)建一個不直接賦值的節(jié)點
$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);
// ------------------------------------------------------
//現(xiàn)在使用xpath從XML文檔中得到內(nèi)容
$doc = xmldoc(join("", file("test_dom.xml")) );
$ctx = xpath_new_context($doc );
分享:XML輕松學(xué)習(xí)手冊(1)XML快速入門前言 XML越來越熱,關(guān)于XML的基礎(chǔ)教程網(wǎng)絡(luò)上也隨處可見�?墒且淮蠖训母拍詈托g(shù)語往往讓人望而生畏,很多朋友問我:XML到底有什么用,我們是否需要學(xué)習(xí)它?我想就我個人學(xué)習(xí)過程的心得和經(jīng)驗,
- 相關(guān)鏈接:
- 教程說明:
Xml教程-使用PHP DOM-XML創(chuàng)建和解析XML文件
。