为了和FLASH交互,需要直接用php输出xml了.
我常用DomDocument对象,代码如下
PHP写XML方法很多,这里主要介绍一下DOMDocument的用法,跟 JS大体上相同,其实非常简单。
002 共分四个文件,分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_POST方式接收就可以用了
003 //index.php 创建功能
004 formatOutput = true;
014 $root = $doc -> createElement_x('root');//新建节点
015 $index = $doc -> createElement_x('index');//新建节点
016 $url = $doc -> createAttribute('url');//新建属性
017 $patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值
018 $url -> appendChild($patch);//将$patch文本设为$url属性的值
019 $id = $doc -> createAttribute('id');
020 $newsid = $doc -> createTextNode($_id);
021 $id -> appendChild($newsid);
022 $title = $doc -> createAttribute('title');
023 $newstitle = $doc -> createTextNode($_title);
024 $title -> appendChild($newstitle);
025 $content = $doc -> createTextNode($_content);//节点值
026 $author = $doc -> createAttribute('author');
027 $newsauthor = $doc -> createTextNode($_author);
028 $author -> appendChild($newsauthor);
029 $sendtime = $doc -> createAttribute('time');
030 $newssendtime = $doc -> createTextNode($_sendtime);
031 $sendtime -> appendChild($newssendtime);
032 $index -> appendChild($id);//将$id设为index节点的属性,以下类同
033 $index -> appendChild($title);
034 $index -> appendChild($content);
035 $index -> appendChild($url);
036 $index -> appendChild($author);
037 $index -> appendChild($sendtime);
038 $root -> appendChild($index);//设置index为root字节点
039 $doc -> appendChild($root);//设置root为跟节点
040 $doc -> save($xmlpatch);//保存文件
041 echo $xmlpatch . ' has create success';
042 ?>
043
044
045
046
047 XML操作
048
049
050
051
052 //add.php 增加功能(跟index.php文件差不多,主要就是加个load载入跟 $root = $doc -> documentElement获得跟节点
053 formatOutput = true;
063 if($doc -> load($xmlpatch)) {
064 $root = $doc -> documentElement;//获得根节点(root)
065 $index = $doc -> createElement_x('index');
066 $url = $doc -> createAttribute('url');
067 $patch = $doc -> createTextNode($_htmlpatch);
068 $url -> appendChild($patch);
069 $id = $doc -> createAttribute('id');
070 $newsid = $doc -> createTextNode($_id);
071 $id -> appendChild($newsid);
072 $title = $doc -> createAttribute('title');
073 $newstitle = $doc -> createTextNode($_title);
074 $title -> appendChild($newstitle);
075 $content = $doc -> createTextNode($_content);
076 $author = $doc -> createAttribute('author');
077 $newsauthor = $doc -> createTextNode($_author);
078 $author -> appendChild($newsauthor);
079 $sendtime = $doc -> createAttribute('time');
080 $newssendtime = $doc -> createTextNode($_sendtime);
081 $sendtime -> appendChild($newssendtime);
082 $index -> appendChild($id);
083 $index -> appendChild($title);
084 $index -> appendChild($content);
085 $index -> appendChild($url);
086 $index -> appendChild($author);
087 $index -> appendChild($sendtime);
088 $root -> appendChild($index);
089 $doc -> save($xmlpatch);
090 echo $_id . ' has been added in ' . $xmlpatch;
091 } else {
092 echo 'xml file loaded error!';
093 }
094 ?>
095
096
097
098
099 XML操作-添加
100
101
102
103
104 //edit.php 修改功能(这里只修改title属性值 跟节点值)
105 formatOutput = true;
112 if($doc -> load($xmlpatch)) {
113 $root = $doc -> documentElement;
114 $elm = $root -> getElementsByTagName_r('index');
115 $checkexist = 0;
116 foreach ($elm as $new) {
117 if($new -> getAttribute('id') == $_id) {
118 $new -> setAttribute('title', $_title);
119 $new -> nodeValue = $_content;//修改节点值,真是太意外了,没想到跟JS一样直接能赋值...
120 //$new -> removeChild($new -> nodevalue);
121 $checkexist = 1;
122 }
123 }
124 if($checkexist == 0) {
125 echo $_id . ' is not found in ' . $xmlpatch;
126 } else {
127 $doc -> save($xmlpatch);
128 echo $_id . ' has been changed';
129 }
130 } else {
131 echo 'xml file loaded error!';
132 }
133 ?>
134
135
136
137
138 XML操作-修改
139
140
141
142
143 //del.php 删除功能
144 formatOutput = true;
149 if($doc -> load($xmlpatch)) {
150 $root = $doc -> documentElement;
151 $elm = $root -> getElementsByTagName_r('index');
152 foreach ($elm as $new) {
153 if($new -> getAttribute('id') == $_id) {
154 if($root -> removeChild($new)) {
155 echo $_id . ' has been deleted';
156 } else {
157 echo $_id . ' delete failed';
158 }
159 }
160 }
161 $doc -> save($xmlpatch);
162 } else {
163 echo 'xml file loaded error!';
164 }
165 ?>
166
167
168
169
170 XML操作-删除
171
172
173
174
175
176 总结一下,创建跟添加主要用的就是create跟appendChild,create后边跟Element就是创建节点,跟Attribute就 是创建属性,TextNode就是创建值,然后appendChild就是设置从属关系,这么一看非常简单。
177 删除与修改都是用先获得节点列表getElementsByTagName然后foreach遍历想要修改的节点,数据多可能会慢点
正文完