今天写了一个sitemap创建的类文件,用到了PHP的XML生成类DOMDocument(),其实,这个类非常好用,也非常简单,只是标签继承的逻辑问题总会搞的迷糊,所以,今天写了一个简单的小类,类很小,也很简单,主要的目的是帮助我们理清思路,不再在标签的继承上迷糊。

下面是PHP的XML生成类的源码,请参考:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> domxml <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$dot</span><span style="color: #339933">;</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> __construct<span style="color: #009900">(</span><span style="color: #000088">$version</span><span style="color: #339933">,</span><span style="color: #000088">$ecode</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">dot</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> DOMDocument<span style="color: #009900">(</span><span style="color: #000088">$version</span><span style="color: #339933">,</span><span style="color: #000088">$ecode</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//实例化DOM对象</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">dot</span><span style="color: #339933">-></span><span style="color: #004000">formatOutput</span><span style="color: #339933">=</span><span style="color: #009900;font-weight: bold">true</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//格式化输出设置</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> createTag<span style="color: #009900">(</span><span style="color: #000088">$tagName</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//创建标签</span>
		<span style="color: #000088">$tagObj</span><span style="color: #339933">=</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">dot</span><span style="color: #339933">-></span><span style="color: #004000">createElement</span><span style="color: #009900">(</span><span style="color: #000088">$tagName</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$tagObj</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> createAtt<span style="color: #009900">(</span><span style="color: #000088">$attName</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #000088">$attObj</span><span style="color: #339933">=</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">dot</span><span style="color: #339933">-></span><span style="color: #004000">createAttribute</span><span style="color: #009900">(</span><span style="color: #000088">$attName</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$attObj</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> creatContent<span style="color: #009900">(</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//创建内容</span>
		<span style="color: #000088">$contentObj</span><span style="color: #339933">=</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">dot</span><span style="color: #339933">-></span><span style="color: #004000">createTextNode</span><span style="color: #009900">(</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #b1b100">return</span> <span style="color: #000088">$contentObj</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> toTag<span style="color: #009900">(</span><span style="color: #000088">$sonObj</span><span style="color: #339933">,</span><span style="color: #000088">$parentObj</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//标签继承 包括内容继承和标签继承</span>
		<span style="color: #666666;font-style: italic">//子对象,父对象</span>
		<span style="color: #000088">$parentObj</span><span style="color: #339933">-></span><span style="color: #004000">appendChild</span><span style="color: #009900">(</span><span style="color: #000088">$sonObj</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> bigTag<span style="color: #009900">(</span><span style="color: #000088">$tagNmae</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//设置主标签</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">dot</span><span style="color: #339933">-></span><span style="color: #004000">appendChild</span><span style="color: #009900">(</span><span style="color: #000088">$tagNmae</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
 
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> saveXml<span style="color: #009900">(</span><span style="color: #000088">$xmlName</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//保存文件到路径</span>
		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">dot</span><span style="color: #339933">-></span><span style="color: #004000">save</span><span style="color: #009900">(</span><span style="color: #000088">$xmlName</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #000000;font-weight: bold">?></span>