设为首页收藏本站language→→ 语言切换

鸿鹄论坛

 找回密码
 论坛注册

QQ登录

先注册再绑定QQ

查看: 1511|回复: 0
收起左侧

如何使用PHP DOM创建动态的XML文件

[复制链接]
发表于 2010-2-24 13:44:06 | 显示全部楼层 |阅读模式
<DIV class=text>当处理基于XML应用程序时,开发者经常需要建立XML编码数据结构。例如,Web中基于用户输入的XML状态模板,服务器请求XML语句,以及基于运行时间参数的客户响应。</DIV><DIV id=endText>< align=left>尽管XML数据结构的构建比较费时,但如果使用成熟的PHP DOM应用程序接口,一切都会变得简单明了。本文将向你介绍PHP DOM应用程序接口的主要功能,演示如何生成一个正确的XML完整文件并将其保存到磁盘中。</P>< align=left><STRONG>创建文档类型声明</STRONG><STRONG></STRONG></P>< align=left>一般而言,XML声明放在文档顶部。在PHP中声明十分简单:只需实例化一个DOM文档类的对象并赋予它一个版本号。查看<STRONG>程序清单</STRONG><STRONG>A</STRONG><STRONG>:</STRONG></P>< align=left><STRONG>程序清单</STRONG><STRONG> A</STRONG></P>< class=textblue align=left>&lt;?php<BR>// create doctype<BR>$dom = new DOMDocument("1.0");<BR><BR>// display document in browser as plain text <BR>// display document in browser as plain text <BR>// for readability purposes<BR>header("Content-Type: text/plain");<BR><BR>// save_and_display tree<BR>echo $dom-&gt;saveXML();<BR>?&gt;</P>< align=left>请注意DOM文档对象的<EM>saveXML()</EM>方法。稍后我再详细介绍这一方法,现在你只需要简单认识到它用于输出XML文档的当前快照到一个文件或浏览器。在本例,为增强可读性,我已经将ASCII码文本直接输出至浏览器。在实际应用中,可将以text/XML头文件发送到浏览器。</P>< align=left>如在浏览器中查看输出,你可看到如下代码:</P>< class=textblue align=left>&lt;?xml version="1.0"?&gt;</P>< align=left><STRONG>添加元素和文本节点</STRONG><STRONG></STRONG></P>< align=left>XML真正强大的功能是来自其元素与封装的内容。幸运的是,一旦你初始化DOM文档,很多操作变得很简单。此过程包含如下两步骤:</P><UL><LI>对想添加的每一元素或文本节点,通过元素名或文本内容调用DOM文档对象的<EM>createElement</EM>()或<EM>createTextNode()</EM>方法。这将创建对应于元素或文本节点的新对象。 <LI>通过调用节点的<EM>appendChild()</EM>方法,并把其传递给上一步中创建的对象,并在XML文档树中将元素或文本节点添加到父节点。</LI></UL>< align=left>以下范例将清楚地演示这2步骤,请查看<STRONG>程序清单</STRONG><STRONG>B</STRONG>。</P>< align=left><STRONG>程序清单</STRONG><STRONG> B</STRONG></P>< class=textblue align=left>&lt;?php<BR>// create doctype<BR>$dom = new DOMDocument("1.0");<BR><BR>// display document in browser as plain text <BR>// for readability purposes<BR>header("Content-Type: text/plain");<BR><BR>// create root element<BR>$root = $dom-&gt;createElement("toppings");<BR>$dom-&gt;appendChild($root);<BR><BR>// create child element<BR>$item = $dom-&gt;createElement("item");<BR>$root-&gt;appendChild($item);<BR><BR>// create text node<BR>$text = $dom-&gt;createTextNode("pepperoni");<BR>$item-&gt;appendChild($text);<BR><BR>// save_and_display tree<BR>echo $dom-&gt;saveXML();<BR>?&gt; </P>< class=textblack align=left>这里,我首先创建一个名字为&lt;toppings&gt;的根元素,并使它归于XML头文件中。然后,我建立名为&lt;item&gt;的元素并使它归于根元素。最后,我又创建一个值为&#8220;pepperoni&#8221;的文本节点并使它归于&lt;item&gt;元素。最终结果如下:</P>< class=textblue align=left>&lt;?xml version="1.0"?&gt;<BR>&lt;toppings&gt;<BR>?&lt;item&gt;pepperoni&lt;/item&gt;<BR>&lt;/toppings&gt;</P>< class=textblack align=left>如果你想添加另外一个topping,只需创建另外一个&lt;item&gt;并添加不同的内容,如程序清单C所示。</P>< class=textblack align=left><STRONG>程序清单C</STRONG></P>< class=textblue align=left>&lt;?php<BR>// create doctype<BR>$dom = new DOMDocument("1.0");<BR><BR>// display document in browser as plain text <BR>// for readability purposes<BR>header("Content-Type: text/plain");<BR><BR>// create root element<BR>$root = $dom-&gt;createElement("toppings");<BR>$dom-&gt;appendChild($root);<BR><BR>// create child element<BR>$item = $dom-&gt;createElement("item");<BR>$root-&gt;appendChild($item);<BR><BR>// create text node<BR>$text = $dom-&gt;createTextNode("pepperoni");<BR>$item-&gt;appendChild($text);<BR><BR>// create child element<BR>$item = $dom-&gt;createElement("item");<BR>$root-&gt;appendChild($item);<BR><BR>// create another text node<BR>$text = $dom-&gt;createTextNode("tomato");<BR>$item-&gt;appendChild($text);<BR><BR>// save_and_display tree<BR>echo $dom-&gt;saveXML();<BR>?&gt;</P>< class=textblack align=left>以下是执行程序清单C后的输出:</P>< class=textblue align=left>&lt;?xml version="1.0"?&gt;<BR>&lt;toppings&gt;<BR>?&lt;item&gt;pepperoni&lt;/item&gt;<BR>?&lt;item&gt;tomato&lt;/item&gt;<BR>&lt;/toppings&gt;</P>< align=left><STRONG>添加属性</STRONG><STRONG></STRONG></P>< align=left>通过使用属性,你也可以添加适合的信息到元素。对于PHP DOM API,添加属性需要两步:首先用DOM文档对象的<EM>createAttribute()</EM>方法创建拥有此属性名字的节点,然后将文档节点添加到拥有属性值的属性节点。详见程序清单D。</P>< align=left><STRONG>程序清单</STRONG><STRONG> D</STRONG></P>< class=textblue align=left>&lt;?php<BR>// create doctype<BR>$dom = new DOMDocument("1.0");<BR><BR>// display document in browser as plain text <BR>// for readability purposes<BR>header("Content-Type: text/plain");<BR>// create root element<BR>$root = $dom-&gt;createElement("toppings");<BR>$dom-&gt;appendChild($root);<BR><BR>// create child element<BR>$item = $dom-&gt;createElement("item");<BR>$root-&gt;appendChild($item);<BR><BR>// create text node<BR>$text = $dom-&gt;createTextNode("pepperoni");<BR>$item-&gt;appendChild($text);<BR><BR>// create attribute node<BR>$price = $dom-&gt;createAttribute("price");<BR>$item-&gt;appendChild($price);<BR><BR>// create attribute value node<BR>$priceValue = $dom-&gt;createTextNode("4");<BR>$price-&gt;appendChild($priceValue);<BR><BR>// save_and_display tree<BR>echo $dom-&gt;saveXML();<BR>?&gt;</P>< align=left>输出如下所示:</P>< class=textblue align=left>&lt;?xml version="1.0"?&gt;<BR>&lt;toppings&gt;<BR>?&lt;item price="4"&gt;pepperoni&lt;/item&gt;<BR>&lt;/toppings&gt;</P>< align=left><STRONG>添加</STRONG><STRONG>CDATA</STRONG><STRONG>模块和过程向导</STRONG><STRONG></STRONG></P>< align=left>虽然不经常使用CDATA模块和过程向导,但是通过调用DOM文档对象的<EM>createCDATASection()</EM>和<EM>createProcessingInstruction()</EM>方法<EM>,</EM> PHP API 也能很好地支持CDATA和过程向导,请见程序清单E。</P>< align=left><STRONG>程序清单</STRONG><STRONG> E</STRONG></P>< class=textblue align=left>&lt;?php<BR>// create doctype<BR>// create doctype<BR>$dom = new DOMDocument("1.0");<BR><BR>// display document in browser as plain text <BR>// for readability purposes<BR>header("Content-Type: text/plain");<BR><BR>// create root element<BR>$root = $dom-&gt;createElement("toppings");<BR>$dom-&gt;appendChild($root);<BR><BR>// create child element<BR>$item = $dom-&gt;createElement("item");<BR>$root-&gt;appendChild($item);<BR><BR>// create text node<BR>$text = $dom-&gt;createTextNode("pepperoni");<BR>$item-&gt;appendChild($text);<BR><BR>// create attribute node<BR>$price = $dom-&gt;createAttribute("price");<BR>$item-&gt;appendChild($price);<BR><BR>// create attribute value node<BR>$priceValue = $dom-&gt;createTextNode("4");<BR>$price-&gt;appendChild($priceValue);<BR><BR>// create CDATA section<BR>$cdata = $dom-&gt;createCDATASection(" Customer requests that pizza be sliced into 16 square pieces ");<BR>$root-&gt;appendChild($cdata);<BR><BR>// create PI<BR>$pi = $dom-&gt;createProcessingInstruction("pizza", "bake()");<BR>$root-&gt;appendChild($pi);<BR><BR>// save_and_display tree<BR>echo $dom-&gt;saveXML();<BR>?&gt;</P>< align=left>输出如下所示:</P>< class=textblue align=left>&lt;?xml version="1.0"?&gt;<BR>&lt;toppings&gt;<BR>  &lt;item price="4"&gt;pepperoni&lt;/item&gt;<BR>  &lt;![CDATA[<BR>      Customer requests that pizza be sliced into 16 square pieces<BR>]]&gt;<BR>  &lt;?pizza bake()?&gt;<BR>&lt;/toppings&gt;</P>< align=left><STRONG>保存结果</STRONG></P>< align=left>一旦已经实现你的目标,就可以将结果保存在一个文件或存储于PHP的变量。通过调用带有文件名的<EM>save()</EM>方法可以将结果保存在文件中,而通过调用<EM>saveXML()</EM>方法可存储于PHP的变量。请参考以下实例(程序清单F):</P>< align=left><STRONG>程序清单</STRONG><STRONG> F</STRONG></P>< class=textblue>&lt;?php<BR>// create doctype<BR>$dom = new DOMDocument("1.0");<BR><BR>// create root element<BR>$root = $dom-&gt;createElement("toppings");<BR>$dom-&gt;appendChild($root);<BR>$dom-&gt;formatOutput=true;<BR><BR>// create child element<BR>$item = $dom-&gt;createElement("item");<BR>$root-&gt;appendChild($item);<BR><BR>// create text node<BR>$text = $dom-&gt;createTextNode("pepperoni");<BR>$item-&gt;appendChild($text);<BR><BR>// create attribute node<BR>$price = $dom-&gt;createAttribute("price");<BR>$item-&gt;appendChild($price);<BR><BR>// create attribute value node<BR>$priceValue = $dom-&gt;createTextNode("4");<BR>$price-&gt;appendChild($priceValue);<BR><BR>// create CDATA section<BR>$cdata = $dom-&gt;createCDATASection(" Customer requests that pizza be</P>< class=textblue align=left>sliced into 16 square pieces ");<BR>$root-&gt;appendChild($cdata);<BR><BR>// create PI<BR>$pi = $dom-&gt;createProcessingInstruction("pizza", "bake()");<BR>$root-&gt;appendChild($pi);<BR><BR>// save tree to file<BR>$dom-&gt;save("order.xml");<BR><BR>// save tree to string<BR>$order = $dom-&gt;save("order.xml");<BR>?&gt;</P>< align=left>希望你能在本文发现有用的东西,并在以后使用XML时应用到这些方法。</P></DIV>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2024-4-25 02:14 , Processed in 0.058420 second(s), 9 queries , Redis On.  

  Powered by Discuz!

  © 2001-2024 HH010.COM

快速回复 返回顶部 返回列表