我有一個XML文件,並希望用php將數據插入到數據庫中。 XML文件的示例是:PHP致命錯誤:調用非對象上的成員函數item()
<shops>
<shop>
<code>HM</code>
<name>Beauty</name>
<address>
<address_name>George St </address_name>
<city>New York</city>
</address>
</shop>
</shops>
我想要的代碼,名稱,ADDRESS_NAME和城市值插入到我的PHP數據庫。
我的PHP代碼:
$xmlDoc = new DOMDocument();
$xmlDoc->load("shops.xml");
$xmlObject = $xmlDoc->getElementsByTagName('shop');
$itemCount = $xmlObject->length;
for ($i=0; $i < $itemCount; $i++) {
$name = $xmlObject->item($i)->getElementsByTagName('name')->item(0)->childNodes->item(0)->nodeValue;
$code = $xmlObject->item($i)->getElementsByTagName('code')->item(0)->childNodes->item(0)->nodeValue;
$sql = "INSERT INTO `shop` (name,code) VALUES ('$name', '$code')";
mysql_query($sql);
}
$xmlObject_n = $xmlDoc->getElementsByTagName('address');
for ($i=0; $i < $itemCount; $i++) {
$address_n = $xmlObject_n->item($i)->getElementsByTagName('address_name')->item(0)->childNodes->item(0)->nodeValue;
$city = $xmlObject_n->item($i)->getElementsByTagName('city')->item(0)->childNodes->item(0)->nodeValue;
$sql_n = "INSERT INTO `place` (address,city) VALUES ('$address_n', '$city')";
mysql_query($sql_n);
}
雖然在第一個for循環錶店的數據正確加載,我獲得了第二個循環以下錯誤:
PHP Fatal error: Call to a member function item() on a non-object
不任何人有任何想法如何解決這個問題,以獲得元素地址的值?
非常感謝
在'$ xmlDoc->負載卸下中'「' (「shops」.xml「);'**太多引號**單獨突出顯示的代碼應該告訴您 – RiggsFolly
我沒有看到任何標籤'number'或'siteid'。 –
@MarcB編輯一個好主意嗎?你確定這只是一個錯字 – RiggsFolly