[請益] 如何使用array來存XML檔

看板PHP作者 (彭太君)時間13年前 (2012/11/16 19:17), 編輯推噓0(003)
留言3則, 2人參與, 最新討論串1/1
請問一下,最近遇到一個問題,要使用array寫入XML 下網查了一下範例程式,發現做法都是把array的值寫死無法修改 可是我是要把從網路上抓下的資料存成array再轉成XML 所以沒辦法寫死,查過許多資料發現最後存的XML都會發生資料覆蓋的問題 例如說有三筆資料,但是最後只有最後一筆資料有成功 前面兩筆都被蓋掉。像是這個樣子 <?xml version="1.0" encoding="utf-8"?> <Pois> <poi> <title>中山基督長老教會</title> <introduction>中山基督長老教會所屬建築落成於1937年</introduction> </poi> </Pois> 想請教各位幫我看看我的城市是哪裡寫錯了,感謝! 程式碼如下 for($i=0 ; $i<count($Wiki_poi) ; $i++) { for($j=0 ; $j<count($Wiki_poi[$i]); $j++) { $Pois = array(); $Pois [] = array( 'title' => $Wiki_poi[$i][$j], 'introduction' => $Wiki_introduction[$i][$j], ); // create doctype $doc = new DOMDocument('1.0', 'utf-8'); $doc->formatOutput = true; // create rootElement $r = $doc->createElement( "Pois" ); $doc->appendChild( $r ); foreach( $Pois as $poi ) { $b = $doc->createElement( "poi" ); // create title $title = $doc->createElement( "title" ); $title->appendChild( $doc->createTextNode( $poi['title'] ) ); $b->appendChild( $title ); // create introduction $introduction = $doc->createElement( "introduction" ); $introduction->appendChild( $doc->createTextNode( $poi['introduction'] ) ); $b->appendChild( $introduction ); $r->appendChild( $b ); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.129.20.128

11/17 00:36, , 1F
- - 你在for 迴圈裡面create xml...?
11/17 00:36, 1F

11/17 00:37, , 2F
每個poi 都重產一次= =...?
11/17 00:37, 2F

11/19 11:30, , 3F
解決了~原來是for迴圈的問題!感謝提示
11/19 11:30, 3F
文章代碼(AID): #1GfY3VAT (PHP)
文章代碼(AID): #1GfY3VAT (PHP)