我想將xml文件中的數據保存到我的數據庫中,並且在下面您可以看到我的php代碼,我嘗試創建一個關聯數組以將值存入以備後用。但是,實際的SimpleXMLElement對象是存儲的,而不是純粹的值。將數據從節點保存到數組
我有兩個問題在這裏:
- 我怎樣才能改變我的代碼,使得實際值(如數字或字符串)存儲,而不是SimpleXMLElement對象?
- 這是一個很好的存儲值的方法,以便以後用SQLLite存儲(我將使用它),或者我有哪些其他選項可以更好地工作?
PHP:
$theProducers = simplexml_load_file('sources/producers.xml');
$i = 0;
foreach ($theProducers->producer as $producer) {
$producers['id'][$i] = $producer->attributes();
$producers['name'][$i] = $producer->name;
$producers['address'][$i] = $producer->address;
$producers['zipcode'][$i] = $producer->zipcode;
$producers['town'][$i] = $producer->town;
$producers['url'][$i] = $producer->url;
$producers['imgurl'][$i] = $producer->imgurl;
$i += 1;
}
print_r($producers); // outcome below
producers.xml:從print_r的
<?xml version="1.0"?>
<producers>
<producer id="8">
<name>Emåmejeriet</name>
<address>Grenvägen 1-3</address>
<zipcode>577 39</zipcode>
<town>Hultsfred</town>
<url>http://www.emamejeriet.se</url>
<imgurl>http://172.16.206.1/~thajo/1DV449/laboration01/producenter/images/ema.png</imgurl>
</producer>
<producer>
...
結果($生產者):
Array (
[id] => Array (
[0] => SimpleXMLElement Object ([@attributes] => Array ([id] => 8))
[1] => SimpleXMLElement Object ([@attributes] => Array ([id] => 57))
[2] => SimpleXMLElement Object ([@attributes] => Array ([id] => 45))
[3] => SimpleXMLElement Object ([@attributes] => Array ([id] => 33))
[4] => SimpleXMLElement Object ([@attributes] => Array ([id] => 16))
[5] => SimpleXMLElement Object ([@attributes] => Array ([id] => 41))
[6] => SimpleXMLElement Object ([@attributes] => Array ([id] => 38))
[7] => SimpleXMLElement Object ([@attributes] => Array ([id] => 40))
[8] => SimpleXMLElement Object ([@attributes] => Array ([id] => 56)))
[name] => Array (
[0] => SimpleXMLElement Object ([0] => Emåmejeriet)
[1] => SimpleXMLElement Object ([0] => Ölands Örtagård AB)
[2] => SimpleXMLElement Object ([0] => Dövestads utegrisar & Gårdsbutik)
... and so on...
你應該像這樣添加索引$ producer-> name [0]。儘管第二個人不知道什麼。 – povilasp