作者ycpeng0515 (彭太君)
看板PHP
标题[请益] 如何使用array来存XML档
时间Fri Nov 16 19:17:48 2012
请问一下,最近遇到一个问题,要使用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
1F:→ qwertmn:- - 你在for 回圈里面create xml...? 11/17 00:36
2F:→ qwertmn:每个poi 都重产一次= =...? 11/17 00:37
3F:→ ycpeng0515:解决了~原来是for回圈的问题!感谢提示 11/19 11:30