当处理基于XML应用程序时,开发者经常需要建立XML编码数据结构。例如,Web中基于用户输入的XML状态模板,服务器请求XML语句,以及基于运行时间参数的客户响应。
尽管XML数据结构的构建比较费时,但如果使用成熟的PHP DOM应用程序接口,一切都会变得简单明了。本文将向你介绍PHP DOM应用程序接口的主要功能,演示如何生成一个正确的XML完整文件并将其保存到磁盘中。
创建文档类型声明
一般而言,XML声明放在文档顶部。在PHP中声明十分简单:只需实例化一个DOM文档类的对象并赋予它一个版本号。查看程序清单A:
程序清单 A
复制代码 代码如下:
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// save and display tree
echo $dom->saveXML();
?>
复制代码 代码如下:
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// save and display tree
echo $dom->saveXML();
?>
复制代码 代码如下:
复制代码 代码如下:
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create another text node
$text = $dom->createTextNode("tomato");
$item->appendChild($text);
// save and display tree
echo $dom->saveXML();
?>
复制代码 代码如下:
复制代码 代码如下:
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// save and display tree
echo $dom->saveXML();
?>
复制代码 代码如下:
复制代码 代码如下:
// create doctype
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(" Customer requests that pizza be sliced into 16 square pieces ");
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);
// save and display tree
echo $dom->saveXML();
?>
复制代码 代码如下:
Customer requests that pizza be sliced into 16 square pieces
]]>
复制代码 代码如下:
// create doctype
$dom = new DOMDocument("1.0");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
$dom->formatOutput=true;
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(" Customer requests that pizza be
sliced into 16 square pieces ");
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);
// save tree to file
$dom->save("order.xml");
// save tree to string
$order = $dom->save("order.xml");
?>
复制代码 代码如下:
$conn = mysql_connect('localhost', 'root', '123456') or die('Could not connect: ' . mysql_error());
mysql_select_db('vdigital', $conn) or die ('Can\'t use database : ' . mysql_error());
$str = "SELECT id,username FROM `admin` GROUP BY `id` ORDER BY `id` ASC";
$result = mysql_query($str) or die("Invalid query: " . mysql_error());
if($result)
{
$xmlDoc = new DOMDocument();
if(!file_exists("01.xml")){
$xmlstr = "
$xmlDoc->loadXML($xmlstr);
$xmlDoc->save("01.xml");
}
else { $xmlDoc->load("01.xml");}
$Root = $xmlDoc->documentElement;
while ($arr = mysql_fetch_array($result)){
$node1 = $xmlDoc->createElement("id");
$text = $xmlDoc->createTextNode(iconv("GB2312","UTF-8",$arr["id"]));
$node1->appendChild($text);
$node2 = $xmlDoc->createElement("name");
$text2 = $xmlDoc->createTextNode(iconv("GB2312","UTF-8",$arr["username"]));
$node2->appendChild($text2);
$Root->appendChild($node1);
$Root->appendChild($node2);
$xmlDoc->save("01.xml");
}
}
mysql_close($conn);
?>
复制代码 代码如下:
$xmlDoc = new DOMDocument();
$xmlDoc->load("http://localhost/xml/xml.php");
$x=$xmlDoc->getElementsByTagName('name');
for ($i=0; $ilength-1; $i++)
{
if(strpos($x->item($i)->nodeValue,"fang")!==false)
{
echo $x->item($i)->parentNode->childNodes->item(1)->nodeValue;
}
}
?>