PHP中的生成XML文件的4种方法

14 篇文章 0 订阅

1. [代码][PHP]代码     

001 <?xml version="1.0" encoding="utf-8"?>
002 <article>
003     <item>
004         <title size="1">title1</title>
005         <content>content1</content>
006         <pubdate>2009-10-11</pubdate>
007     </item>
008     <item>
009         <title size="1">title2</title>
010         <content>content2</content>
011         <pubdate>2009-11-11</pubdate>
012     </item>
013 </article>
014  
015 【直接生成字符串】
016 方法1:使用纯粹的PHP代码生成字符串,并把这个字符串写入一个以XML为后缀的文件。这是最原始的生成XML的方法,不过有效!
017 PHP代码如下:
018  
019 <?PHP
020 $data_array array(
021     array(
022     'title' => 'title1',
023     'content' => 'content1',
024         'pubdate' => '2009-10-11',
025     ),
026     array(
027     'title' => 'title2',
028     'content' => 'content2',
029     'pubdate' => '2009-11-11',
030     )
031 );
032 $title_size = 1;
033  
034 $xml "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
035 $xml .= "<article>\n";
036  
037 foreach ($data_array as $data) {
038 $xml .= create_item($data['title'], $title_size$data['content'], $data['pubdate']);
039 }
040  
041 $xml .= "</article>\n";
042  
043 echo $xml;
044  
045 //  创建XML单项
046 function create_item($title_data$title_size$content_data$pubdate_data)
047 {
048     $item "<item>\n";
049     $item .= "<title size=\"" $title_size "\">" $title_data "</title>\n";
050     $item .= "<content>" $content_data "</content>\n";
051     $item .= " <pubdate>" $pubdate_data "</pubdate>\n";
052     $item .= "</item>\n";
053  
054     return $item;
055 }
056  
057 ?>
058  
059 【DomDocument】
060 方法2:使用DomDocument生成XML文件
061 创建节点使用createElement方法,
062 创建文本内容使用createTextNode方法,
063 添加子节点使用appendChild方法,
064 创建属性使用createAttribute方法
065 PHP代码如下:
066  
067 <?PHP
068 $data_array array(
069     array(
070     'title' => 'title1',
071     'content' => 'content1',
072         'pubdate' => '2009-10-11',
073     ),
074     array(
075     'title' => 'title2',
076     'content' => 'content2',
077     'pubdate' => '2009-11-11',
078     )
079 );
080  
081 //  属性数组
082 $attribute_array array(
083     'title' => array(
084     'size' => 1
085     )
086 );
087  
088 //  创建一个XML文档并设置XML版本和编码。。
089 $dom=new DomDocument('1.0''utf-8');
090  
091 //  创建根节点
092 $article $dom->createElement('article');
093 $dom->appendchild($article);
094  
095 foreach ($data_array as $data) {
096     $item $dom->createElement('item');
097     $article->appendchild($item);
098  
099     create_item($dom$item$data$attribute_array);
100 }
101  
102 echo $dom->saveXML();
103  
104 function create_item($dom$item$data$attribute) {
105     if (is_array($data)) {
106         foreach ($data as $key => $val) {
107             //  创建元素
108             $$key $dom->createElement($key);
109             $item->appendchild($$key);
110  
111             //  创建元素值
112             $text $dom->createTextNode($val);
113             $$key->appendchild($text);
114  
115             if (isset($attribute[$key])) {
116             //  如果此字段存在相关属性需要设置
117                 foreach ($attribute[$keyas $akey => $row) {
118                     //  创建属性节点
119                     $$akey $dom->createAttribute($akey);
120                     $$key->appendchild($$akey);
121  
122                     // 创建属性值节点
123                     $aval $dom->createTextNode($row);
124                     $$akey->appendChild($aval);
125                 }
126             }   //  end if
127         }
128     }   //  end if
129 }   //  end function
130 ?>
131  
132 【XMLWriter】
133 方法3:使用XMLWriter类创建XML文件
134 此方法在PHP 5.1.2后有效
135 另外,它可以输出多种编码的XML,但是输入只能是utf-8
136 PHP代码如下:
137  
138 <?PHP
139 $data_array array(
140     array(
141     'title' => 'title1',
142     'content' => 'content1',
143         'pubdate' => '2009-10-11',
144     ),
145     array(
146     'title' => 'title2',
147     'content' => 'content2',
148     'pubdate' => '2009-11-11',
149     )
150 );
151  
152 //  属性数组
153 $attribute_array array(
154     'title' => array(
155     'size' => 1
156     )
157 );
158  
159 $xml new XMLWriter();
160 $xml->openUri("php://output");
161 //  输出方式,也可以设置为某个xml文件地址,直接输出成文件
162 $xml->setIndentString('  ');
163 $xml->setIndent(true);
164  
165 $xml->startDocument('1.0''utf-8');
166 //  开始创建文件
167 //  根结点
168 $xml->startElement('article');
169  
170 foreach ($data_array as $data) {
171     $xml->startElement('item');
172  
173     if (is_array($data)) {
174         foreach ($data as $key => $row) {
175           $xml->startElement($key);
176  
177           if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
178           {
179               foreach ($attribute_array[$keyas $akey => $aval) {
180               //  设置属性值
181                     $xml->writeAttribute($akey$aval);
182                 }
183  
184             }
185  
186             $xml->text($row);   //  设置内容
187             $xml->endElement(); // $key
188         }
189  
190     }
191     $xml->endElement(); //  item
192 }
193  
194 $xml->endElement(); //  article
195 $xml->endDocument();
196  
197 $xml->flush();
198 ?>
199  
200 【SimpleXML】
201 方法4:使用SimpleXML创建XML文档
202  
203 <?PHP
204 $data_array array(
205     array(
206     'title' => 'title1',
207     'content' => 'content1',
208         'pubdate' => '2009-10-11',
209     ),
210     array(
211     'title' => 'title2',
212     'content' => 'content2',
213     'pubdate' => '2009-11-11',
214     )
215 );
216  
217 //  属性数组
218 $attribute_array array(
219     'title' => array(
220     'size' => 1
221     )
222 );
223  
224 $string = <<<XML
225 <?xml version='1.0' encoding='utf-8'?>
226 <article>
227 </article>
228 XML;
229  
230 $xml = simplexml_load_string($string);
231  
232 foreach ($data_array as $data) {
233     $item $xml->addChild('item');
234     if (is_array($data)) {
235         foreach ($data as $key => $row) {
236           $node $item->addChild($key$row);
237  
238           if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
239             {
240               foreach ($attribute_array[$keyas $akey => $aval) {
241              //  设置属性值
242                   $node->addAttribute($akey$aval);
243             }
244           }
245         }
246     }
247 }
248 echo $xml->asXML();
249 ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值