001 | <?xml version= "1.0" encoding= "utf-8" ?> |
004 | <title size= "1" >title1</title> |
005 | <content>content1</content> |
006 | <pubdate>2009-10-11</pubdate> |
009 | <title size= "1" >title2</title> |
010 | <content>content2</content> |
011 | <pubdate>2009-11-11</pubdate> |
016 | 方法1:使用纯粹的PHP代码生成字符串,并把这个字符串写入一个以XML为后缀的文件。这是最原始的生成XML的方法,不过有效! |
023 | 'content' => 'content1' , |
024 | 'pubdate' => '2009-10-11' , |
028 | 'content' => 'content2' , |
029 | 'pubdate' => '2009-11-11' , |
034 | $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" ; |
035 | $xml .= "<article>\n" ; |
037 | foreach ( $data_array as $data ) { |
038 | $xml .= create_item( $data [ 'title' ], $title_size , $data [ 'content' ], $data [ 'pubdate' ]); |
041 | $xml .= "</article>\n" ; |
046 | function create_item( $title_data , $title_size , $content_data , $pubdate_data ) |
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" ; |
060 | 方法2:使用DomDocument生成XML文件 |
061 | 创建节点使用createElement方法, |
062 | 创建文本内容使用createTextNode方法, |
064 | 创建属性使用createAttribute方法 |
071 | 'content' => 'content1' , |
072 | 'pubdate' => '2009-10-11' , |
076 | 'content' => 'content2' , |
077 | 'pubdate' => '2009-11-11' , |
082 | $attribute_array = array ( |
089 | $dom = new DomDocument( '1.0' , 'utf-8' ); |
092 | $article = $dom ->createElement( 'article' ); |
093 | $dom ->appendchild( $article ); |
095 | foreach ( $data_array as $data ) { |
096 | $item = $dom ->createElement( 'item' ); |
097 | $article ->appendchild( $item ); |
099 | create_item( $dom , $item , $data , $attribute_array ); |
104 | function create_item( $dom , $item , $data , $attribute ) { |
105 | if ( is_array ( $data )) { |
106 | foreach ( $data as $key => $val ) { |
108 | $ $key = $dom ->createElement( $key ); |
109 | $item ->appendchild($ $key ); |
112 | $text = $dom ->createTextNode( $val ); |
113 | $ $key ->appendchild( $text ); |
115 | if (isset( $attribute [ $key ])) { |
117 | foreach ( $attribute [ $key ] as $akey => $row ) { |
119 | $ $akey = $dom ->createAttribute( $akey ); |
120 | $ $key ->appendchild($ $akey ); |
123 | $aval = $dom ->createTextNode( $row ); |
124 | $ $akey ->appendChild( $aval ); |
133 | 方法3:使用XMLWriter类创建XML文件 |
135 | 另外,它可以输出多种编码的XML,但是输入只能是utf-8 |
142 | 'content' => 'content1' , |
143 | 'pubdate' => '2009-10-11' , |
147 | 'content' => 'content2' , |
148 | 'pubdate' => '2009-11-11' , |
153 | $attribute_array = array ( |
159 | $xml = new XMLWriter(); |
160 | $xml ->openUri( "php://output" ); |
162 | $xml ->setIndentString( ' ' ); |
163 | $xml ->setIndent(true); |
165 | $xml ->startDocument( '1.0' , 'utf-8' ); |
168 | $xml ->startElement( 'article' ); |
170 | foreach ( $data_array as $data ) { |
171 | $xml ->startElement( 'item' ); |
173 | if ( is_array ( $data )) { |
174 | foreach ( $data as $key => $row ) { |
175 | $xml ->startElement( $key ); |
177 | if (isset( $attribute_array [ $key ]) && is_array ( $attribute_array [ $key ])) |
179 | foreach ( $attribute_array [ $key ] as $akey => $aval ) { |
181 | $xml ->writeAttribute( $akey , $aval ); |
201 | 方法4:使用SimpleXML创建XML文档 |
207 | 'content' => 'content1' , |
208 | 'pubdate' => '2009-10-11' , |
212 | 'content' => 'content2' , |
213 | 'pubdate' => '2009-11-11' , |
218 | $attribute_array = array ( |
225 | <?xml version= '1.0' encoding= 'utf-8' ?> |
230 | $xml = simplexml_load_string( $string ); |
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 ); |
238 | if (isset( $attribute_array [ $key ]) && is_array ( $attribute_array [ $key ])) |
240 | foreach ( $attribute_array [ $key ] as $akey => $aval ) { |
242 | $node ->addAttribute( $akey , $aval ); |
|